3
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a sequence of numbers ending with 0.");

    ArrayList<Integer> list = new ArrayList<Integer>();

    String num = scan.nextLine();

    for(int x=0; x < num.length(); x++){
        System.out.println(num.charAt(x));

        int y = num.charAt(x);
        System.out.println(y);
        list.add(y);
        System.out.println(list);


    } 

Im trying to cast a string of numbers into a array. Its not adding the correct vaule. I keep getting 49 and 50. I want to store the numbers the user enters into the ArrayList. Can someone help?

2
  • 1
    That is because its giving you the ASCII value, int y = num.charAt(x)-48 or Character.valueOf(num.charAt(x)) since '0' is represented by 48, refer: asciitable.com Commented May 12, 2015 at 4:59
  • @Thilo and my answer will give you expected result. Commented May 12, 2015 at 5:31

5 Answers 5

2
 int y = num.charAt(x);

That will give you the Unicode codepoint for the character. Like 65 for A or 48 for 0.

You probablay want

 int y = Integer.parseInt(num.substring(x, x+1));
Sign up to request clarification or add additional context in comments.

Comments

0

You can try to use:

int y = Integer.parseInt(num.charAt(x));

instead of

int y = num.charAt(x);

1 Comment

Integer.parseInt is not applicable for char as a method argument. You code will give compilation error.
0

You are not converting the input to Integer, thus JVM is taking them as string. Assuming you are 1 as you input it is printing 49 (ASCII equivalent) of "1".

If you want to get the integral values, you need to parse it using

int y = Integer.parseInt(num.charAt(x));
System.out.println(y);
list.add(y);
System.out.println(list);

1 Comment

Integer.parseInt is not applicable for char as a method argument. You code will give compilation error.
0

As this code int y = num.charAt(x); is creating the problem. As you are trying to store returned character into int value, so it is storing ASCII value of the character.

You can go with the suggestions in other answers.


For the simplicity, you can rewrite your code like this.

Scanner scan = new Scanner(System.in);
System.out.println("Enter a sequence of numbers ending with 0.");

ArrayList<Integer> list = new ArrayList<Integer>();

String num = scan.nextLine();

char[] charArray = num.toCharArray();
for (char c : charArray) {
    if (Character.isDigit(c)) {
        int y = Character.getNumericValue(c);
        System.out.println(y);
        list.add(y);
        System.out.println(list);
    } else {
         // you can throw exception or avoid this value.
    }
}

Note: Integer.valueOf and Integer.parseInt will not give proper result for char as a method argument. You need to pass String as a method argument in both the cases.

Comments

0

You are copying a char into an int. You need to convert it into an int value.

int y = Character.getNumericValue(num.charAt(x));

5 Comments

Your method is taking integer as an input (Integer valueOf(int i)), so in this case it will return ASCII value.
check this out docs.oracle.com/javase/7/docs/api/java/lang/… besides num.charAt(x) does not return an int.
That is for String, not for char. In your code, it is passing char, so internally it will call valueOf(int i) method, and it will pass ASCII value of char (returned by num.charAt(x)) as a method argument. You can check by executing your code.
You are right, valueOf method doesn't give the correct value;
Your code can work like this int y = Integer.valueOf(String.valueOf(num.charAt(x)));. Refer note part of my answer to this question.

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.