Converting a String Array to an Int array [closed]

2019-09-22 12:16发布

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);
}

2条回答
等我变得足够好
2楼-- · 2019-09-22 12:44

How about

String[] lines = ...
int[] ints = new int[lines.length];
for(int i = 0; i < lines.length; i++)
    ints[i] = Integer.parseInt(lines[i]);
查看更多
倾城 Initia
3楼-- · 2019-09-22 12:58

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
    }
}
查看更多
登录 后发表回答