Splitting String and put it on int array
Solution 1
For input 1,2,3,4,5
the input is of length 9. 9/2 = 4
in integer math, so you're only storing the first four variables, not all 5.
Even if you fixed that, it would break horribly if you passed in an input of 10,11,12,13
It would work (by chance) if you used 1,2,3,4,50
for an input, strangely enough :-)
You would be much better off doing something like this
String[] strArray = input.split(",");
int[] intArray = new int[strArray.length];
for(int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
For future reference, when you get an error, I highly recommend posting it with the code. You might not have someone with a jdk readily available to compile the code to debug it! :)
Solution 2
Java 8 offers a streams-based alternative to manual iteration:
int[] intArray = Arrays.stream(input.split(","))
.mapToInt(Integer::parseInt)
.toArray();
Be prepared to catch NumberFormatException
if it's possible for the input to contain character sequences that cannot be converted to an integer.
Solution 3
Let's consider that you have input as "1,2,3,4".
That means the length of the input is 7. So now you write the size = 7/2 = 3.5. But as size is an int
, it will be rounded off to 3. In short, you are losing 1 value.
If you rewrite the code as below it should work:
String input;
int length, count, size;
Scanner keyboard = new Scanner(System.in);
input = keyboard.next();
length = input.length();
String strarray[] = input.split(",");
int intarray[] = new int[strarray.length];
for (count = 0; count < intarray.length ; count++) {
intarray[count] = Integer.parseInt(strarray[count]);
}
for (int s : intarray) {
System.out.println(s);
}
user1076331
Updated on October 20, 2020Comments
-
user1076331 over 3 years
I have to input a string with numbers ex: 1,2,3,4,5. That's a sample of the input, then I have to put that in an array of INT so I can sort it but is not working the way it should work.
package array; import java.util.Scanner; public class Array { public static void main(String[] args) { String input; int length, count, size; Scanner keyboard = new Scanner(System.in); input = keyboard.next(); length = input.length(); size = length / 2; int intarray[] = new int[size]; String strarray[] = new String[size]; strarray = input.split(","); for (count = 0; count < intarray.length ; count++) { intarray[count] = Integer.parseInt(strarray[count]); } for (int s : intarray) { System.out.println(s); } } }
-
Ray Britton over 11 yearsI'd recommend you use this instead: numbers[i] = Integer.parseInt(strings[i].trim()) otherwise if the string was " 4,22,42" it would throw an exception.
-
corsiKa about 11 yearsThe divide by 2 is there because of the assumption that for each number, there is a corresponding comma. Of course, we know this is a flawed assumption, but I'm sure that was OP's line of thinking at the time.
-
Kartik Chugh over 7 years@Downvoters, at least comment the reason for downvoting.
-
Patrick Parker about 6 years@K_7 " input a string with numbers ex:
1,2,3,4,5
" not "ABCD" -
Guru raj almost 6 yearsNote For Android Developer, you also need min SDK version 24 along with Java 8
-
Ruzihm over 5 yearsThis doesn't answer the question. The question is about creating an int array, not a
List<Integer>
.