-1

[It's not about converting a String into an Integer]

I need to take some digits from a String that is a line by console command.

For Example :

String str = "234 432 22 66 8 44 7 4 3 333";

How can I get every single Integer value and put them in an array? The order of digits is not important because the String could be:

String str = "34 434343 222";

or

String str = " 1 2 3 4 5 6 7";

Also, how can I get digits in these two cases (with one or more whitespace characters):

String str = "2 2 44 566";

and

 String str = "2121     23  44 55 6   58";
3

4 Answers 4

6

If you want to capture the numbers you have separated by spaces, then you can do:

String str = "234 432 22 66 8 44 7 4 3 333";

String[] strArr = str.split("\\s+");
// strArr => ["234", "432", "22", "66", "8", "44", "7", "4", "3", "333"]

Update: as Evan LaHurd pointed in his comment, then you can work on the array values and if you want to transform the string into integers you can use:

int n = Integer.parseInt("1234");
// or
Integer x = Integer.valueOf("1234");

IDEOne Example

3
  • 2
    Then if you want to use the Integer objects, you can use Integer.parseInt(String s) on each String in the array. Sep 18, 2015 at 19:38
  • so i can work with a number of spaces >1 only using "\\s+" ?
    – tucossss
    Sep 18, 2015 at 19:53
  • @f.stacchietti exactly, the \s+ is a regular expression to match 1 to many spaces and use them to split your string. In java you have to escape the backslash, hence \\s+ Sep 18, 2015 at 19:55
0

You can do it like this.

    String str = "234 432 22 66 8 44 7 4 3 333";

    String[] stringArray = str.trim().split("\\s+");//remove any leading, trailing white spaces and split the string from rest of the white spaces

    int[] intArray = new int[stringArray.length];//create a new int array to store the int values

    for (int i = 0; i < stringArray.length; i++) {
        intArray[i] = Integer.parseInt(stringArray[i]);//parse the integer value and store it in the int array
    }
2
  • This will fail on leading space (see 3rd example in question).
    – Andreas
    Sep 18, 2015 at 19:43
  • yeah it goes!! thanks for the help, i spent the last 1 hour for this, haha. Thank you again!
    – tucossss
    Sep 18, 2015 at 19:54
0

Use split on spaces and parse the substrings to integers:

String str = "   2121     23  44 55 6   58   ";

// To list
List<Integer> numberList = new ArrayList<Integer>();
for (String numberText : str.trim().split("\\s+"))
    numberList.add(Integer.valueOf(numberText));

// To array
String[] numberTexts = str.trim().split("\\s+");
int[] numberArray = new int[numberTexts.length];
for (int i = 0; i < numberTexts.length; i++)
    numberArray[i] = Integer.parseInt(numberTexts[i]);

// Show result
System.out.println(numberList);                   // [2121, 23, 44, 55, 6, 58]
System.out.println(Arrays.toString(numberArray)); // [2121, 23, 44, 55, 6, 58]
0

if you just want to put the numbers of the string in an array, you can also use the "nextInt()" method from the Scanner class.

String str = "2 56  73     4           9   10";
Scanner scanner = new Scanner(str);

List<Integer> integers = new ArrayList<Integer>(0);
While( scanner.hasNext() )   // checks if theres another number to add
    integers.add( scanner.nextInt() );

you can also avoid none-numeral characters using "hasNextInt()":

while( scanner.hasNext(){
    if( scanner.hasNextInt() )
        integers.add( scanner.nextInt() );
}

remember that if you want it to become an array you will still need to convert the list or put the numbers together in a different way.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.