-3

Well the question is very simple. Lets say there is a StringBuilder sb Inside of sb numbers like this:

"25 9631 12 1895413 1 234564"

How to get those numbers one by one and initialize them into an ArrayList<Integer> list or to be more specific is it possible? Thanks for checking!

doppler
  • 53
  • 1
  • 1
  • 6
  • Google for "split string in Java". Then google for "parse integer in Java". – JB Nizet Apr 18 '15 at 13:23
  • 1
    Just convert to String via `toString()` and split that String. Closed as a duplicate of *many* similar posts. – Hovercraft Full Of Eels Apr 18 '15 at 13:24
  • `String string = new StringBuilder("25 9631 12 1895413 1 234564").toString(); String[] numbersStrings = string.split("\\s"); List list = new ArrayList<>(); for(String num : numbersStrings) { list.add(Integer.parseInt(num)); }` – EpicPandaForce Apr 18 '15 at 13:25

2 Answers2

2

Try to split that string like:

String[] numbers = sb.toString().split(" ");//if spaces are uneven, use \\s+ instead of " "
for (String number : numbers) {
    list.add(Integer.valueOf(number));
}
SMA
  • 36,381
  • 8
  • 49
  • 73
  • hey doppler , if you are working on something huge then for(String numbers : numbers) also called as a foreach statement may not be very good . Try using a regular for loop , for(int i = 0 ; i < numbers.length ; i ++) and use list.add(Integer.parseInt(numbers[i])); – TheAnimatrix Apr 18 '15 at 13:51
  • 1
    @TheAnimatrix This is not true. When iterating over arrays the *extended for* (which is what a for-each is officially called) gets expanded into a *regular for* loop similar to yours automatically. See the language specs [here](http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14.2) and this answer [here](http://stackoverflow.com/a/256861/5717099). – morido Jan 10 '16 at 12:37
  • @morido Well i missed that part , glad to have that info . thanks :) – TheAnimatrix Jan 10 '16 at 12:39
1

Well my solution may not be the best here ,

From your question , what i've understood is that basically you want to get all the numbers from a string builder and put it in an integer array.

So here goes ,

Firstly you may want to get a string from the string builder.

String myString = mystringbuilder.toString();

This string now contains your numbers with spaces.

now use the following ,

String[] stringIntegers = myString.split(" "); // " " is a delimiter , a space in your case

This string array now contains your integers at positions starting from 0 .

Now , you may want to take this string array and parse its values and put it in an ArrayList.

This is how it's done ,

ArrayList<Integer> myIntegers = new ArrayList<Integer>();    
for(int i = 0; i<stringIntegers.length;i++){
myIntegers.add(Integer.parseInt(stringIntegers[i]));
}

now your myIntegers arraylist is populated with the needed integers , let me break it down for you.

  1. You create an array for the integers.
  2. There's a for loop to cycle through the string array
  3. In this for loop for every position of the string array you convert the string at that position to an integer with Integer.parseInt(String);
  4. Then , you just add it to the arraylist we created.

COMPLETE CODE:

String mynumbers = stringBuilder.toString();
String[] stringIntegers = mynumbers.split(" ");
ArrayList<Integer> myIntegers = new ArrayList<Integer>();
for(int i=0;i<stringIntegers.length;i++){
myIntegers.add(Integer.parseInt(stringIntegers[i]));
}
TheAnimatrix
  • 566
  • 1
  • 6
  • 19