0

I'm doing some Java homework and I can't get this while loop to work correctly.

I've tried to editing the code, but it keeps giving errors.

while(true) {
            System.out.println("Is student a TA? (1: YES 0: NO): ");
                status = inReader.nextInt();
                    if (status == 1) {
                        status1 = true;
                    }
                    else if (status == 0) {
                            status1 = false;
                    }
                    else {
                        System.out.println("Please enter valid entry.");
                    } 
            Graduate grad = new Graduate(studentID, name, major, status1);
            grad.displayStudentData();
                }   
            }
        }
   }

I want the code to loop back if the user inputs anything other than a 0 or 1. It does loop back but the information gets outputted to the console. Which is not what I was hoping it would do.

2
  • Do you know about continue and break? Inserting one of each into your code in the right place will make it behave like you want to. Commented Jul 24, 2019 at 16:04
  • Why not wrap only the reading in the while loop? Creation and output can be done outside. Commented Jul 25, 2019 at 4:14

5 Answers 5

1

I'm going to assume that what you are doing with the grad variable is the information you don't want displayed when you get an invalid answer. I'm also assuming you eventually want to exit the loop.

boolean doLoop = true;
Graduate grad = null;

while(doLoop) {
        System.out.println("Is student a TA? (1: YES 0: NO): ");
        status = inReader.nextInt();

        if (status == 1) {
            grad = new Graduate(studentID, name, major, true);
            doLoop  = false;
        }
        else if (status == 0) {
            grad = new Graduate(studentID, name, major, false);
            doLoop  = false;
        }
        else {
            System.out.println("Please enter valid entry.");
        } 

        if (grad != null){
            grad.displayStudentData();
        }
    }
}

Checking for a null allows you to only display something when there's something to display.

Getting rid of the status1variable reduces the number of variables you need to mentally keep track of. That variable isn't necessary anyway, since there are better ways to deal with validating an input.

Setting the doLoop variable allows you to exit the while when you need to.

Also, the code you posted had a few extra close brackets. It's a good idea to make sure the code you post is either runnable or pseudo-code.

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

Comments

1

I suggest that you take a step back from writing code and describe the steps in English (or any other written human language). For example, you can write something like this:

while input is invalid
    get input
    check if input is valid
create a Graduate object
display data

This is called "pseudocode". Note how I use indentation similar to how we write code to indicate what steps are repeated in the while loop. This also shows how the last two steps should not be inside the while loop.

If you want to be able to do this for multiple students then there should be another loop around this one. Again, write the steps in words to figure out the exact structure that you need.

Comments

0

What you have going on is that once input that is not 1 or 0 is inputted, you will execute the code in the else block. Then your code will execute

Graduate grad = new Graduate(studentID, name, major, status1);
grad.displayStudentData();

If you didn't want that to be executed, add continue; to the end of the else block.

Comments

0

Solution:

boolean run = true;
while(run){

    boolean valid = true;
    System.out.println("Is student a TA? (1: YES 0: NO): ");
    status = inReader.nextInt();
    boolean status;
    if (status == 1) {
        status1 = true;
        run = false;
    }
    else if (status == 0) {
        status1 = false;
        run = false;
    }
    else {
        System.out.println("Please enter valid entry.");
        valid = false;
    } 
    if(valid){ //or if(valid == true)
        Graduate grad = new Graduate(studentID, name, major, status1);
        grad.displayStudentData();
    }
}  

2 Comments

"But you can also write it like this: while(run == true)" No, don't do that.
This is continually setting the valid variable as well as defaulting to an untrue state. If you use this method, it is better to set the valid variable outside the loop to false and only set it to true when it's actually valid for it to be true.
0

You say it yourself,

I want the code to loop back if the user inputs anything other than a 0 or 1

so why not put that in the condition?

// initialize with invalid value so we enter the loop at least once
int input = -1;
while (input != 0 && input != 1) {
    System.out.println("Is student a TA? (1: YES 0: NO): ");
    input = inReader.nextInt();
}
// now you're guaranteed to have a 0 or 1 in the input
boolean status = input == 1; // will the true on 1, false on 0
Graduate grad = new Graduate(studentID, name, major, status);
grad.displayStudentData();

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.