0
Scanner input = new Scanner(System.in);
    System.out.println("How many numbers will you enter?");
    int x = input.nextInt();
    input.nextLine();
    int[] array = new int[x];
    
    System.out.println("Enter " + x + " integers, one per line:");
    Integer y = input.nextInt();
    input.nextLine();
    String z = y.toString();
    
    for (int i = 0; i < array.length; i++) {
        array[i] = z.charAt(i);
        System.out.println(z.charAt(i));
    }
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }

I put 1 to feed into the array and when I printed it, it got printed 49 instead.

0

1 Answer 1

3

You put a char into an int array, and you got the expected result -- which is the ASCII value of the character '1', or 49.

What you want to do for your use case is to use Integer.parseInt on the String of the entire line, not to try to convert the individual characters.

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

Comments

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.