Converting a String Array to an Int array [closed]

流过昼夜 提交于 2019-12-13 11:30:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!