我收到一個奇怪的異常代碼。StringIndexOutOfBoundsException:字符串索引超出範圍:0
,我嘗試使用的代碼如下:
do
{
//blah blah actions.
System.out.print("\nEnter another rental (y/n): ");
another = Keyboard.nextLine();
}
while (Character.toUpperCase(another.charAt(0)) == 'Y');
錯誤代碼是:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
at Store.main(Store.java:57)
57號線是啓動一個 「而...」。
請幫忙,這是駕駛我巴蒂!
另一個被實例化爲「Y」。即使它被實例化爲「YES」,我也有同樣的錯誤。 – Chente 2009-12-18 08:45:32
Chente - 如果'another'是空字符串,則此錯誤只能**發生。更仔細地檢查你的斷言(理想情況下用一個調試器,以便你可以看到究竟發生了什麼)和/或實施Itay的建議,看看問題如何消失。 – 2009-12-18 08:58:54