Integer.parseInt(String);
是你想要的東西。
試試這個:
int[] array = new int[size];
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int j = 0; j < array.length ; j++) {
int k = Integer.parseInt(br.readLine());
array[j] = k;
}
}
catch (Exception e) {
e.printStackTrace();
}
不管怎麼說,你爲什麼不使用掃描儀?如果您使用Scanner,對您而言會更容易。 :)
int[] array = new int[size];
try {
Scanner in = new Scanner(System.in); //Import java.util.Scanner for it
for (int j = 0; j < array.length ; j++) {
int k = in.nextInt();
array[j] = k;
}
}
catch (Exception e) {
e.printStackTrace();
}
如果'String'不包含可分析的'int',則不要忘記捕獲'NumberFormatException'。 – Asaph 2009-12-21 05:53:15