你能幫我解答嗎? 我有這樣的代碼這是我的計劃的一部分:「」如何添加回車到這個字符串數組?
Scanner a5 = new Scanner(System.in);
while(a5.hasNextLine()) //store every line in mail message until user type "." on a seperate line
{
String text = a5.nextLine();
if (text.charAt(0) != '.')
{txt.add(text);}
else if(text.charAt(0) == '.' )
{break;}
}
我想用戶鍵入消息,直到他不鍵入在另一行中,他可以添加更多行。但我的問題是,當我按下進入我得到這個錯誤:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 0
at java.lang.String.charAt(Unknown Source)
at MyPMC.main(MyPMC.java:51)
我如何解決它,所以它也是空行添加到我的字符串? 例如: 當我在中鍵入這一點,好的工作:
Hi
my name is jack
.
但是當我按第二句,我得到錯誤,我的程序崩潰後進入,我不能像這樣輸入:
Hi
My name is jack
this is message
.
謝謝
我仍然得到錯誤,當我按下輸入,我可以這樣鍵入: – raymond
嗨,我的名字是傑克然後空行,然後這是結束然後。 – raymond
你在兩個不同的地方訪問charAt(0),你是否糾正了它們兩個? – kviiri