0

I'm trying to use string input (yes or no) as a sentinel for a loop. The loop repeats a switch statement, and at the end prompts the user with a yes or no question. Until they type 'yes' as their answer, the loop continues to receive data from the user. The problem comes from trying to receive a new value for the sentinel.

***Reviewed the comments and made some changes. Here's the updated code:

    Scanner input = new Scanner ( System.in );

    System.out.print( "Please enter a product number, 1 - 5: ");
    int product = input.nextInt();
    double sum = 0;
    boolean complete = false;

    while (!complete) {

    switch (product){
        case 1: sum = sum + 2.98;
        break;
        case 2: sum = sum + 4.50;
        break;
        case 3: sum = sum + 9.98;
        break;
        case 4: sum = sum + 4.49;
        break;
        case 5: sum = sum + 6.87;
        break;
    }
    System.out.print( "Is your order complete? Please type true or false:");
    complete = in.nextLine();
    }

All of that is working but I am still having trouble with the prompt to break the sentinel. I'm trying to get it set up to where the user types true to end the loop or false to continue it. I'm starting to think I've overlooked something. I greatly appreciate the help, thank you.

1
  • Does this even compile? It looks as if complete is a String, and you can't have a while (!<String variable>). It's just not allowed. Commented Sep 16, 2011 at 21:27

4 Answers 4

2

Change

    default: String complete = input.Stream(); //Not coded, but will also ask for input.

to

    default: complete = input.Stream(); //Not coded, but will also ask for input.

You're redeclaring a String, which is not what you want. And follow the advice given of using .equals, not == for Strings or other reference types.

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

Comments

0

It's griping because you declared it twice. Take off the second String declaration:

  default: complete = input.Stream(); //Not coded, but will also ask for input.

Comments

0

You are declaring complete twice. Try modifying your default case statement as:

default: complete = input.Stream()

Comments

0

Two things:

1) you are redeclaring complete. The default case should just be:

default: complete = input.Stream();

2) in Java you can't compare Strings with !=. Every String is an object and by using the comparison operators you are comparing the identifiers of the objects, not the contents of the objects. Java will never see two String objects as equal unless they are two strings pointing to the same memory location. To compare the values of two strings you have to use the .equals() method such as:

while (!complete.equals("yes")) {

Or even better as a habit:

while (!"yes".equals(complete)) {

This is better because if the complete String variable were null then the first comparison would throw a null pointer exception where the second won't since the literal string "yes" will always evaluate to a not-null object.

Hope that helps.

David

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.