Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert an integer string separated by space into an array in JAVA

Suppose I have a string "1 23 40 187 298". This string only contains integers and spaces. How can I convert this string to an integer array, which is [1,23,40,187,298]. this is 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 doesn't work, please offer some help. Thank you!

like image 337
Learn and practice Avatar asked Mar 23 '15 20:03

Learn and practice


People also ask

How do you take space separated integers in Java?

There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each value. Using nextInt( ) method of Scanner class.

How do I convert a string to an int array?

The string. split() method is used to split the string into various sub-strings. Then, those sub-strings are converted to an integer using the Integer. parseInt() method and store that value integer value to the Integer array.

How do you take space separated integer inputs?

To take space-separated integers from user input:Use the input() function to take multiple, space-separated integers. Use the str. split() function to split the string into a list. Use the int() class to convert each string in the list to an integer.

Can we store string and integer together in an array?

An Object[] can hold both String and Integer objects. Here's a simple example: Object[] mixed = new Object[2]; mixed[0] = "Hi Mum"; mixed[1] = Integer.


1 Answers

You are forgetting about

  • resetting temp to empty string after you parse it to create place for new digits
  • that at the end of your string will be no space, so

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

    will not be invoked, which means you need invoke

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

    once again after your loop


But simpler way would be just using split(" ") to create temporary array of tokens and then parse each token to int like

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();

Other approach could be using Scanner and its nextInt() method to return all integers from your input. With assumption that you already know the size of needed 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();
}
like image 194
Pshemo Avatar answered Sep 18 '22 13:09

Pshemo