[問題] 如何在字串中讀取字元
剛剛寫到一題
題目會給好幾串英數字
"1A3B4S\n"
"4X1b1X\n"
" ......."
然後要輸出
"ABBBSSSS\n"
"XXXXbX\n "
"....... "
只是我不知道要怎麼單獨去讀取個別的字串出來運算
上網查了一下好像是用charAt();
先寫了下面的程式碼測試
Scanner scanner = new Scanner(System.in);
String str1 = scanner.nextLine();
for(int i=0;i<132;i++) \\每一列不會超過132個字元
{
int num = str1.charAt(2*i);
System.out.print(num);
}
但是他卻顯示
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: 4
4850 at java.lang.String.charAt(String.java:658)
請問是哪裡出問題了呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.161.23.175
※ 文章網址: https://www.ptt.cc/bbs/java/M.1445438123.A.5F8.html
推
10/21 22:55, , 1F
10/21 22:55, 1F
推
10/21 23:00, , 2F
10/21 23:00, 2F
→
10/21 23:00, , 3F
10/21 23:00, 3F
→
10/21 23:01, , 4F
10/21 23:01, 4F
→
10/21 23:06, , 5F
10/21 23:06, 5F
→
10/21 23:07, , 6F
10/21 23:07, 6F
推
10/21 23:42, , 7F
10/21 23:42, 7F
→
10/22 00:28, , 8F
10/22 00:28, 8F
→
10/22 00:31, , 9F
10/22 00:31, 9F
推
10/22 02:07, , 10F
10/22 02:07, 10F
推
10/22 02:46, , 11F
10/22 02:46, 11F
推
10/22 02:49, , 12F
10/22 02:49, 12F
推
10/22 08:23, , 13F
10/22 08:23, 13F
→
10/22 08:58, , 14F
10/22 08:58, 14F
推
10/22 15:31, , 15F
10/22 15:31, 15F
→
10/22 17:38, , 16F
10/22 17:38, 16F
→
10/22 17:39, , 17F
10/22 17:39, 17F
→
10/22 17:40, , 18F
10/22 17:40, 18F
推
10/22 19:49, , 19F
10/22 19:49, 19F
→
10/22 19:49, , 20F
10/22 19:49, 20F
→
10/22 19:50, , 21F
10/22 19:50, 21F
→
10/22 19:50, , 22F
10/22 19:50, 22F
→
10/22 19:51, , 23F
10/22 19:51, 23F
→
10/22 19:53, , 24F
10/22 19:53, 24F
→
10/22 19:53, , 25F
10/22 19:53, 25F
→
10/22 19:53, , 26F
10/22 19:53, 26F
→
10/22 19:54, , 27F
10/22 19:54, 27F
推
10/23 19:02, , 28F
10/23 19:02, 28F
推
10/25 18:11, , 29F
10/25 18:11, 29F
推
10/30 02:18, , 30F
10/30 02:18, 30F
推
10/30 22:33, , 31F
10/30 22:33, 31F
java 近期熱門文章
PTT數位生活區 即時熱門文章