0

I need help figuring out how to get the user to input a number of integers no more than 10, and then add them to an array and print them out from the array. The code I have below, when run, asks the user for the integers and then runs forever and doesn't work. What am I doing wrong?

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); // create a new scanner
    System.out.print("Enter integers between 1 and 100\n ");  
    int[] nextNumber = new int[10];
    int i = 0;
    int number = input.nextInt();

    while (i < nextNumber.length){
        i++;
        nextNumber[i] = number;
        number = input.nextInt();    
    }

    int a = 0;

    while (a  < nextNumber.length){
        a++;
        System.out.println(nextNumber[a]);   
    }
11
  • System.out.println(nextNumber[a]); You should a++ after the System.out.println(nextNumber[a]); Commented Oct 22, 2013 at 3:21
  • wasn't the problem, i had forgot to fix that from when i was experimenting with why it wont print. but even when the bottom line is System.out.println(nextNumber[a]); it still doesnt work Commented Oct 22, 2013 at 3:23
  • what do you mean by "does not work"? Also swap a++ and println Commented Oct 22, 2013 at 3:24
  • Add some print statements between steps, inside your while blocks if you need to. Or debug it in your IDE. Where is it looping forever? Commented Oct 22, 2013 at 3:36
  • 1
    No, nextNumber.length will be 10, because that's what you initialized it as, no matter how many elements are populated. Commented Oct 22, 2013 at 3:47

4 Answers 4

2

Seems to me like you increment your index too fast. You should increment your index variables at the end of your loops, not the beginning.

I would suggest you use for loops instead since they are designed for that:

Scanner input = new Scanner(System.in); // create a new scanner
System.out.print("Enter integers between 1 and 100\n ");

int[] nextNumber = new int[10];

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

for (int a = 0; a  < nextNumber.length; a++){
    System.out.println(nextNumber[1]);
}

Also, although I did not change it in the code, it seems like your last line should be:

System.out.println(nextNumber[a]);
Sign up to request clarification or add additional context in comments.

Comments

1

Increment the array index after the values have been assigned to the arrays

while (i < nextNumber.length) {
    number = input.nextInt();
    nextNumber[i] = number;
    i++;
}

The same applys to the second loop

while (a < nextNumber.length) {
    System.out.println(nextNumber[a]);
    a++;
}

2 Comments

still has the same problem. my program continues to run with nothing outputting to the screen
Actually the original problem was an ArrayIndexOutofBoundsException but worked just fine there, silly question: are you running the current version, clean/rebuild etc?
0

Problem 1

Change

int i = 0;
int number = input.nextInt();
while (i < nextNumber.length){
i++;  //here is one problem. you not assigning the value to nextNumber[0].
nextNumber[i] = number;
number = input.nextInt();

}

to

int i = 0;
while (i < nextNumber.length){
   number = input.nextInt();
   nextNumber[i] = number;
   i++;  
}

Problem 2

change

int a = 0;
while (a  < nextNumber.length){
    a++;  //here is one problem ..You never get  nextNumber[0] value
System.out.println(nextNumber[a]);

}

to

int a = 0;
while (a  < nextNumber.length){
     System.out.println(nextNumber[a]);
    a++;
}

3 Comments

sorry, i noticed that too, but isn't the problem. Still doesnt run :/
still has the same problem. my program continues to run with nothing outputting to the screen
@nfoggia you need to give 10 inputs one by one. type one number then press Enter key. do it 10 times
0

You can easily do this as follows. Better use for loop since you know the maximum number of iterations.

Scanner input = new Scanner(System.in); // create a new scanner
System.out.print("Enter integers between 1 and 100\n ");  
int[] nextNumber = new int[10];

int i = 0;
while (i < nextNumber.length)   nextNumber[i++] =  input.nextInt();


int a = 0;
while (a  < nextNumber.length)  System.out.println(nextNumber[a++]);  

2 Comments

Make it clear & correct first, long before you make it super-concise. I like the loop-body on a separate line; and definitely prefer advancement/increment to have it's own line for clarity. The OP here is a beginner, suggesting the most-terse C-style when he can't even place his increments correctly is not guiding him down the path to clear & simple correct code.
What you are saying is true Thomas. It can be simplify by separating them for line by line. But it does the same job. I wanted to illustrate the optimal code for his example.

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.