0

I have a string with this content 12345.

What's the best way to read these individual numbers into an ArrayList of integers? like this:

{1, 2, 3, 4, 5}

Here is what I have so far:

String data = getData();
this.points=new ArrayList<Integer>();
for (int i=0; i<data.length(); i++) {
    int pt = Integer.parseInt(data.valueOf(i));
    this.points.add(new Integer(pt));
}

The problem is that I get [(0),(1),(2),...] instead of the numbers required.

1
  • 2
    Lookup what String.valueOf does - it does not do what you think it does. Commented Mar 26, 2013 at 14:34

7 Answers 7

4

You need to use the charAt(i) method, valueOf(i) returns you the String value of i and not the i char

Sign up to request clarification or add additional context in comments.

Comments

2

This solution should work and keep your memory allocation to a minimum. Note that you need to make sure that only numerical values are present on data.

String data = getData();
this.points = new ArrayList<Integer>(); //if this is a field you should use points.clear() instead.

for (int i=0; i<data.length(); i++) {
  this.points.add(Characacter.getNumericValue(data.charAt(i)));
}

Your problem was that you were trying to use the String.valueOf method on the index, which will essentially return the value of i in every iteration

Comments

2

You can use String.charAt to get the corresponding character as char from the position. To add it to the list you need to convert to int.

String data = getData();
this.points=new ArrayList<Integer>();
List<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < data.length(); i++) {
    this.points.add(Character.getNumericValue(data.charAt(i)));
}

4 Comments

In this case I get strange numbers like [40, 40, 48, 41, 40, 50, 41, 41]
This will return the ASCII values. Use Character.getNumericValue to return the numeric values.
Since you reopened your answer and fixed it I will remove mine, because its the same
@Sednus: Why? There are so many questions that have similar answers.
1

Integer.parseInt will take the whole number; you want to take it digit-by-digit. For this, use Character.getNumericValue instead.

String data = getData();
this.points=new ArrayList<Integer>();
for (int i=0; i<data.length(); i++) {
    int pt = Character.getNumericValue(data.charAt(i));
    this.points.add(new Integer(pt));
}

4 Comments

Are you sure you have actual numbers? If you have a string like "123??5", the result will be [1, 2, 3, -1, -1, 5].
Yes, I'm sure. The string is '12345'.
I'm not sure what the problem is. I tried this myself, with the sample string "12345", and printing pt followed by a space every time. I got "1 2 3 4 5 ".
Though actually, this will handle some Unicode. For example, u216C, the Roman numeral for fifty, will correctly be returned as 50.
0

Use data.charAt() instead of valueOf. So your code should look something like:

        String data = getData();
        for (int i = 0; i < data.length(); i++) {
            int pt = Integer.parseInt(data.valueOf(data.charAt(i)));
            this.points.add(new Integer(pt));
        }
        for (int i = 0; i < points.size(); i++) {
            System.out.println(this.points.get(i));

        }

Comments

0

Try this

String data = getData();
this.points=new ArrayList<Integer>();
for (int i = 0; i < data.length(); i++) {
    this.points.add(Integer.parseInt(s.charAt(i) + ""));
}

Comments

-1

You get all the integers like this:

String data = "254980";
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < data.length(); i++) {
    list.add(new Integer(data.substring(i,i+1)));
}

Test:

for (Integer integer : list) {
    System.out.print(integer);
}

prints

2 
5 
4 
9 
8 
0

2 Comments

This solution wastes memory by creating new instances of strings with the substring method
The suggestion is not to use substring method inside a loop. Each iteration of the loop requires a full re-scan of the entire string.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.