Home > other >  while loop incrementing an extra time
while loop incrementing an extra time

Time:03-17

The below while loop runs an extra time. I am trying to perform a user input that accepts 10 valid numbers from the user and prints their sum. However, the while loop executes an extra time and asks for the 11th input.

 public static void main(String[] args) {
    
    int i = 1, sum = 0;
    Scanner sc = new Scanner(System.in);

    while(i <= 10){
    i  ;
    
    System.out.println("Enter number "   "#"  i);
    boolean isValidNumber = sc.hasNextInt();

    if(isValidNumber){
        int userChoiceNumber = sc.nextInt();
        sum  = userChoiceNumber;
    }else{
        System.out.println("Invalid Input");
    }
   }
  }
   System.out.println("The sum of your entered numbers are = "   sum);

}

CodePudding user response:

In addition to those great comments, you should probably only increment "i" if you get a VALID input:

while(i <= 10) {
  System.out.print("Enter number "   "#"  i   ": ");
  boolean isValidNumber = sc.hasNextInt();
  if(isValidNumber){
    int userChoiceNumber = sc.nextInt();
    sum  = userChoiceNumber;
    i  ;
  }else{
    System.out.println("Invalid Input");
    sc.next();
  }
}

Note that when you have a bad input you need to get rid of it with "sc.next()".

CodePudding user response:

First - make sure you're formatted correctly. (I've indented your loops, moved your output into the main class, fixed up some curly brackets/loop endings).

public static void main(String[] args) {

    int i = 1, sum = 0;
    Scanner sc = new Scanner(System.in);

    while(i <= 10){
        i  ;
    
        System.out.println("Enter number "   "#"  i);
        boolean isValidNumber = sc.hasNextInt();
    
        if(isValidNumber){
            int userChoiceNumber = sc.nextInt();
            sum  = userChoiceNumber;
        }
        else{
            System.out.println("Invalid Input");
        }
    }

    System.out.println("The sum of your entered numbers are = "   sum);
}

Alright - so running the code, I've found there are the correct amount of times asked, but the input propt is displaying the wrong number with the first input prompt starting on 2, the last one on 11.

The reason for this is the i runs before asking for an input, thus it counts up before outputting.

This can easily be fixed by moving said i to just underneath the else clause - as follows:

        else{
            System.out.println("Invalid Input");
        }
        i  
    }

CodePudding user response:

the main problem here is that you're increasing the variable at the start of your while loop. If that's what you're looking for then that's fine, but if you want to stop the loop when it hits 10 you'll need to have it like while(i < 10) if the i were at the end of the loop, then you could do while(i <= 10) Ex:

i = 0;
while(i < 10){
    i  ;
    //code here
}

this will make the code that uses i use the values between 1 and 10. using <= will use the values between 1 and 11.


another example:
i = 0;
while(i < 10){
    //code here
    i  ;
}

this will make the code that uses i use the values between 0 and 9. using <= will use the values between 0 and 10.


another way people do an incremental loop is doing a for loop rather than a while loop this would look like:

for(int i = 0; i < 10; i  ){
    //code here
}

this also allows you to create a variable that will only be inside the loop, so rather than making it at the beginning of the method or before the loop, you could make it inside the for loop. This is not good if the variable is used elsewhere though.

  • Related