Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String Array to an Integer Array

so basically user enters a sequence from an scanner input. 12, 3, 4, etc.
It can be of any length long and it has to be integers.
I want to convert the string input to an integer array.
so int[0] would be 12, int[1] would be 3, etc.

Any tips and ideas? I was thinking of implementing if charat(i) == ',' get the previous number(s) and parse them together and apply it to the current available slot in the array. But I'm not quite sure how to code that.

like image 778
Mario Avatar asked Sep 16 '13 23:09

Mario


People also ask

How do I convert a string array to an integer array?

You can convert a String to integer using the parseInt() method of the Integer class. To convert a string array to an integer array, convert each element of it to integer and populate the integer array with them.

Can you convert a string to an array?

We can also convert String to String array by using the toArray() method of the List class. It takes a list of type String as the input and converts each entity into a string array.


1 Answers

You could read the entire input line from scanner, then split the line by , then you have a String[], parse each number into int[] with index one to one matching...(assuming valid input and no NumberFormatExceptions) like

String line = scanner.nextLine(); String[] numberStrs = line.split(","); int[] numbers = new int[numberStrs.length]; for(int i = 0;i < numberStrs.length;i++) {    // Note that this is assuming valid input    // If you want to check then add a try/catch     // and another index for the numbers if to continue adding the others (see below)    numbers[i] = Integer.parseInt(numberStrs[i]); } 

As YoYo's answer suggests, the above can be achieved more concisely in Java 8:

int[] numbers = Arrays.stream(line.split(",")).mapToInt(Integer::parseInt).toArray();   

To handle invalid input

You will need to consider what you want need to do in this case, do you want to know that there was bad input at that element or just skip it.

If you don't need to know about invalid input but just want to continue parsing the array you could do the following:

int index = 0; for(int i = 0;i < numberStrs.length;i++) {     try     {         numbers[index] = Integer.parseInt(numberStrs[i]);         index++;     }     catch (NumberFormatException nfe)     {         //Do nothing or you could print error if you want     } } // Now there will be a number of 'invalid' elements  // at the end which will need to be trimmed numbers = Arrays.copyOf(numbers, index); 

The reason we should trim the resulting array is that the invalid elements at the end of the int[] will be represented by a 0, these need to be removed in order to differentiate between a valid input value of 0.

Results in

Input: "2,5,6,bad,10"
Output: [2,3,6,10]

If you need to know about invalid input later you could do the following:

Integer[] numbers = new Integer[numberStrs.length]; for(int i = 0;i < numberStrs.length;i++)         {     try      {         numbers[i] = Integer.parseInt(numberStrs[i]);     }     catch (NumberFormatException nfe)        {         numbers[i] = null;     } } 

In this case bad input (not a valid integer) the element will be null.

Results in

Input: "2,5,6,bad,10"
Output: [2,3,6,null,10]


You could potentially improve performance by not catching the exception (see this question for more on this) and use a different method to check for valid integers.

like image 54
Java Devil Avatar answered Sep 30 '22 17:09

Java Devil