How to convert a whole string, space-separated, to an array in JAVA

Suppose I have the line "1 23 40 187 298". This line contains only integers and spaces. How to convert this string to an integer array that is [1,23,40,187,298]. that's how i tried

public static void main(String[] args) { String numbers = "12 1 890 65"; String temp = new String(); int[] ary = new int[4]; int j=0; for (int i=0;i<numbers.length();i++) { if (numbers.charAt(i)!=' ') temp+=numbers.charAt(i); if (numbers.charAt(i)==' '){ ary[j]=Integer.parseInt(temp); j++; } } } 

but it does not work, please offer some help. Thanks!

+9
java string arrays
source share
5 answers

You forgot about

  • reset temp to an empty string after parsing to create space for new digits
  • that there will be no space at the end of your line, therefore

     if (numbers.charAt(i) == ' ') { ary[j] = Integer.parseInt(temp); j++; } 

    will not be called, which means you need to call

     ary[j] = Integer.parseInt(temp); 

    once again after the cycle


But a simpler way would be to simply use split(" ") to create a temporary array of tokens and then parse each token into an int , e.g.

 String numbers = "12 1 890 65"; String[] tokens = numbers.split(" "); int[] ary = new int[tokens.length]; int i = 0; for (String token : tokens){ ary[i++] = Integer.parseInt(token); } 

which can also be shortened with streams added in Java 8 :

 String numbers = "12 1 890 65"; int[] array = Stream.of(numbers.split(" ")) .mapToInt(token -> Integer.parseInt(token)) .toArray(); 

Another approach would be to use Scanner and its nextInt() method to return all integers from your input. Assuming you already know the size of the required array, you can simply use

 String numbers = "12 1 890 65"; int[] ary = new int[4]; int i = 0; Scanner sc = new Scanner(numbers); while(sc.hasNextInt()){ ary[i++] = sc.nextInt(); } 
+13
source share

For java 8+ you can use this method:

 final Integer[] ints = Arrays.stream(numbers.split(" ")) .map(Integer::parseInt) .toArray(Integer[]::new); 

or, if you need primitive ints, you can use this:

 final int[] ints = Arrays.stream(numbers.split(" ")) .mapToInt(Integer::parseInt) .toArray(); 
+7
source share

Reset tmp String to "" after you parse the integer if you do not want to continue adding all line numbers together. There are also alternatives - for example, splitting String into an array on a space character, and then parsing the numbers individually

0
source share

Try it,

 public static void main(String[] args) { String numbers = "12 1 890 65"; String[] parts = numbers.split(" "); int[] ary = new int[4]; int element1 = Integer.parseInt(parts[0]); int element2 = Integer.parseInt(parts[1]); int element3 = Integer.parseInt(parts[2]); int element4 = Integer.parseInt(parts[3]); ary[0] = element1; ary[1] = element2; ary[2] = element3; ary[3] = element4; for(int i=0; i<4; i++){ System.out.println(ary[i]); } } 
0
source share

I met a similar question in Android development. I want to convert a long string into two -String arrays of the xtokens array and int of the ytokens array.

 String result = "201 5 202 8 203 53 204 8"; String[] tokens = result.split(" "); String[] xtokens = new String[tokens.length/2 + 1]; int[] ytokens = new int[tokens.length/2 + 1]; for(int i = 0, xplace = 0, yplace = 0; i<tokens.length; i++){ String temptoken = new String(tokens[i]); if(i % 2 == 0){ xtokens[xplace++] = temptoken; }else { ytokens[yplace++] = Integer.parseInt(temptoken); } } 

You can first convert this string to an array of strings separated by a space, and then convert it to an int array.

0
source share

All Articles