【发布时间】:2015-07-22 19:13:03
【问题描述】:
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sequence of numbers ending with 0.");
ArrayList<Integer> list = new ArrayList<Integer>();
String num = scan.nextLine();
for(int x=0; x < num.length(); x++){
System.out.println(num.charAt(x));
int y = num.charAt(x);
System.out.println(y);
list.add(y);
System.out.println(list);
}
我正在尝试将一串数字转换为数组。它没有添加正确的价值。我不断得到 49 和 50。我想将用户输入的数字存储到 ArrayList 中。有人可以帮忙吗?
【问题讨论】:
-
那是因为它给了你ASCII值,
int y = num.charAt(x)-48或Character.valueOf(num.charAt(x)),因为'0'用48表示,参考:asciitable.com -
@Thilo 我的回答会给你预期的结果。
标签: java string casting integer