问题
I am reading a text file which has numbers in. Each number has its own line, and I can read them in and display them using a println fine. But I am trying to convert them to an integer array so that I can then use the numbers coming in.
String lines[] = loadStrings("movements.txt");
for (int i = 0 ; i < lines.length; i++) {
println(lines[i]);
delay(100);
}
回答1:
How about
String[] lines = ...
int[] ints = new int[lines.length];
for(int i = 0; i < lines.length; i++)
ints[i] = Integer.parseInt(lines[i]);
回答2:
I believe you want to do something like the following:
String[] lines = loadStrings("movements.txt");
int[] ints = new int[lines.length];
for (int i = 0; i < lines.length; i++) {
try {
ints[i] = Integer.parseInt(lines[i]);
} catch (NumberFormatException e) {
//Error handling goes here
}
}
来源:https://stackoverflow.com/questions/21677530/converting-a-string-array-to-an-int-array