0

just want to break out of this while loop. Condition doesn't break when I assumed it will, but it does get registered by the subsequent if statement.

  String current_Class_Name = "";
  int current_Class_Rating;  

    while(!current_Class_Name.equals("Done")) {

      // * Get class name.
      System.out.println("What class are you rating?");
      current_Class_Name = in.nextLine();
      // *
      // if(current_Class_Name.equals("Done")) {
      // System.out.println("Detected 'Done'");
      // break;
      // }

      // * Get class rating.
      System.out.println("How many plus signs does " + current_Class_Name + " get?");
      current_Class_Rating = Integer.parseInt(in.nextLine());

      // * If user inputs an invalid rating (not a value between 0-5),
      // * keep prompting them until they enter a valid input.
      while(current_Class_Rating > 5 || current_Class_Rating < 0) {
        System.out.println("Sorry, but you can only give a rating of 0-5.");
        System.out.println("How many plus signs does " + current_Class_Name + " get?");
        current_Class_Rating = Integer.parseInt(in.nextLine());
      }

      // * TODO
      // * Add to STRING and INTEGER LISTS.
    }


Possibly some kind of call order problem as the String is "empty" upon entering the while loop. Sorry for any formatting.

Thanks for any and all help.

NOTE : I typically use C# and as far as I remember, a while loop will try and detect the condition case before completing an entire iteration.

EDIT : The if statement is not there to run, but to just prove two things : 1) that the break condition can be detected and 2) to break out at a given time.

EDIT : Updated to show all code in consideration.

Edward Suzuki
  • 103
  • 2
  • 9
  • 1
    It does indeed work like your `if`, I can only assume you have **more** code in the loop body after the `if`. That will continue to evaluate until the loop condition is tested again (i.e. when the `while` evaluates after the body completes). – Elliott Frisch Feb 14 '20 at 01:37
  • 1
    You are never giving the `while` condition a chance to check the condition when the current_Class_Name is "Done", because the `if` condition breaks out of the loop before the next iteration when the while _would_ check the condition. – Stephen P Feb 14 '20 at 01:48
  • Updated post with almost all code in consideration and removed some misleading/confusing details. – Edward Suzuki Feb 14 '20 at 01:56
  • 2
    _"as far as I remember, a while loop will try and detect the condition case before completing an entire iteration"_ — No… it checks the condition _on **entry**_ to the loop. If the condition is met the whole loop executes (unless something causes a `break`) then you return to the top of the loop where the condition is once again tested. – Stephen P Feb 14 '20 at 01:57
  • Ah, ok. Just to be clear, the while loop will complete a full iteration before checking the condition as opposed to when the variable is modified right? Thanks a lot for your time. – Edward Suzuki Feb 14 '20 at 02:01
  • 1
    Exactly @EdwardSuzuki - alternatively, you can use `do { ... } while (condition)` to guarantee you execute the loop body at least once, and check the condition at the _end_ of the iteration. – Stephen P Feb 14 '20 at 18:04

2 Answers2

1

Try changing the implementation to:

    String current_Class_Name = null;
    Integer current_Class_Rating = null;

    do {
        // * Get class name.
        System.out.println("What class are you rating?");
        current_Class_Name = in.nextLine().trim();


        try {
            // * Get class rating.
            System.out.println(String.format("How many plus signs does '%s' get?",current_Class_Name));
            current_Class_Rating = Integer.parseInt(in.nextLine().trim());
        }catch(NumberFormatException e) {
            current_Class_Rating = null;
        }

        if((current_Class_Rating == null)||(!(current_Class_Rating>=0 && current_Class_Rating <=5))) {
            System.out.println("Invalid rating value! Rating must be integer 0-5!");
            continue;  // skips back to beginning of the loop
        }

        // * TODO
        // * Add to STRING and INTEGER LISTS.

    }while(!current_Class_Name.equalsIgnoreCase("done"));
Hrisimir Dakov
  • 557
  • 3
  • 9
1

Your question appears to have been based on a misconception, the while loop will only terminate when the condition is re-evaluated to be false at the top (not on the instant of the variable being updated). However, you can make it so the prompt, the assignment and the evaluation happen at once. First, create a helper method. Like,

private static String promptForClassName(Scanner in) {
    System.out.println("What class are you rating?");
    return in.nextLine();
}

Then, use it with the fact that assignment (as a side-effect) evaluates to the assigned value; also, please follow standard Java camel case naming conventions. Like,

String currentClassName;
while (!(currentClassName = promptForClassName(in)).equalsIgnoreCase("Done")) {
    String prompt = "How many plus signs does " + currentClassName + " get?";
    // * Get class rating.
    System.out.println(prompt);
    int currentClassRating = Integer.parseInt(in.nextLine());

    // * If user inputs an invalid rating (not a value between 0-5),
    // * keep prompting them until they enter a valid input.
    while (currentClassRating > 5 || currentClassRating < 0) {
        System.out.println("Sorry, but you can only give a rating of 0-5.");
        System.out.println(prompt);
        currentClassRating = Integer.parseInt(in.nextLine());
    }

    // * TODO
    // * Add to STRING and INTEGER LISTS.
}
System.out.println("Detected 'Done'");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249