0

I am loading a String from a csv file and trying to make an int array with it. The problem is I keep running into a NumberFormatException which is thrown when the program finds a "" in the String array.

I don't need those empty Strings, I just want ints.

Is there a way to avoid replacing characters with empty Strings?

     aLine = aLine.replaceAll(" ", "").replaceFirst(",", "");
     aLine = aLine.replace(name, "").replaceAll("\"", "");

     final String[] strScores = aLine.split(",");

     final int[] scores = Arrays.stream(strScores)
                                 .mapToInt(Integer::parseInt).toArray();
0

2 Answers 2

2

You could filter the stream for not empty and not null before you parse. Like,

final int[] scores = Arrays.stream(strScores)
        .filter(x -> x != null && !x.isEmpty())
        .mapToInt(Integer::parseInt)
        .toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't know you could do that.
0

You should use an array list because you can easily add and remove

array_list<String> list = new ArrayList<String>(); // create array list

for (int i =0; i < array_string.length;i++){
    if (!string_array[i].equals("")){ // filter out empty
        array_list.add(string_array[i]);
    }
}

String new_string_array_without_empty = array_list.toArray(new String[0]); // convert to string array again

2 Comments

Thanks, but I would rather not add any data structures I don't need.
You are right. I am new to android development but decided to try to help anyways. Thanks for replying. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.