-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";
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
tucossss
  • 63
  • 1
  • 1
  • 7
  • 1
    Take a look to [`String.split(String regex)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)). – eric.m Sep 18 '15 at 19:34
  • split the string into numbers and parse each one as an integer. – Peter Lawrey Sep 18 '15 at 19:38
  • possible duplicate of [Converting String to Int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – Ivan1211 Sep 18 '15 at 19:38

4 Answers4

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

Community
  • 1
  • 1
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • 2
    Then if you want to use the `Integer` objects, you can use `Integer.parseInt(String s)` on each `String` in the array. – Evan LaHurd Sep 18 '15 at 19:38
  • so i can work with a number of spaces >1 only using "\\s+" ? – tucossss Sep 18 '15 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+` – Federico Piazza Sep 18 '15 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
    }
shan1024
  • 1,389
  • 7
  • 17
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]
Andreas
  • 154,647
  • 11
  • 152
  • 247
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.