1. 程式人生 > >java.lang.StringIndexOutOfBoundsException: String index out of range: 1

java.lang.StringIndexOutOfBoundsException: String index out of range: 1

筆者原始碼如下:

package javaPractice;

import java.util.*;

public class TestScanner {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        ArrayList<Character> a = new ArrayList<Character>();
        int i = 0;
        while(str.charAt(i)!='#') {
            a.add(str.charAt(i));
            System.out.println((int)a.get(i));
            i ++;
        }
        sc.close();
    }
}

//先佔個坑,小陌早晚會解決的