0

I'm not exactly sure what I have done but my goal is to accept 10 numbers from the user and store them into an array. In which I can then print back out in order received. I believe I have accomplished that with the exception of the first value is defaulting to zero in the output and I am not sure how to fix that. Any help?

package array;
import java.util.Scanner;
import java.util.Arrays;

public class Array {

    public static void main(String[] args) {
        int[] a = new int[10];

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter ten numbers: ");
        input.nextInt();

        for(int j=0; j<10; j++)
            a[j]=input.nextInt();

        System.out.println("Your number list is:");
        System.out.println(Arrays.toString(a));

        }
    }
}
4
  • Thanks, I pasted the wrong code initially. I tried playing around to skip index 0 to see if that made a difference. It didn't of course. Commented Apr 19, 2017 at 1:50
  • 2
    You don't take 10 inputs here, just want to point. Commented Apr 19, 2017 at 1:51
  • it should now I believe. I updated the code Commented Apr 19, 2017 at 1:52
  • @JonathanRoberts You can't enter ten numbers into an array of only nine elements. Commented Apr 19, 2017 at 1:54

2 Answers 2

2

Your array should be sized to 10 (and your loop test should also be 10, or better - use the array length). You should use braces, it helps prevent subtle bugs. And I see no reason to discard the first int. Putting it all together like,

int[] a = new int[10];
Scanner input = new Scanner(System.in);
System.out.println("Please enter ten numbers: ");
for (int j = 0; j < a.length; j++) {
    a[j] = input.nextInt();
}
System.out.println("Your number list is: ");
System.out.println(Arrays.toString(a));
Sign up to request clarification or add additional context in comments.

3 Comments

Forgive me if I am wrong but does the array index not start at zero? So calling for 10 places would actually output 11, no?
@JonathanRoberts Valid indices in the array are 0 to length - 1. So the above has space for ten elements, and valid indices are 0 to 9 inclusive.
I understand now. Thank you!
2

you've defined an array of 9 elements, not 10.

change this:

int[] a = new int[9];

to this:

int[] a = new int[10];

also, change this:

for(int j = 0; j < 9; j++)

to this:

for(int j = 0; j < a.length; j++)

Lastly but not least these two statements should not be inside the loop:

System.out.println("Your number list is: ");
System.out.println(Arrays.toString(a));

place them outside the loop.

for (int j = 0; j < a.length; j++) {
     a[j] = input.nextInt();
}

System.out.println("Your number list is:");
System.out.println(Arrays.toString(a));

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.