我试图找到字符串的倒数第二个字符。我尝试使用word.length() -2
,但我收到一个错误。我用java如何查找字符串的倒数第二个字符?
String Word;
char c;
lc = word.length()-1;
slc = word.length()-2; // this is where I get an error.
System.out.println(lc);
System.out.println(slc);//error
异常线程 “main” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 在java.lang.String.charAt(来源不明) 在snippet.hw5 .main(hw5.java:30)
请向我们显示您的代码。 –
你想要倒数第二还是最后一个字? – RandomQuestion
请告诉我们你在说什么语言!向我们显示代码,复制确切的错误消息。这太笼统了,无法回答。 –