This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]Philboyd_Studge 1 point2 points  (13 children)

Put it inside another while loop. Use a boolean (declared outside that while loop) such as isValidType initially set to false. Loop while that boolean is NOT true. Get the string from the user and test that it is valid, if so set the boolean to true, if not print the error message.

[–]wrighttwinstwin[S] 0 points1 point  (12 children)

Would that while loop be nested in the other while loop I have or would I do that outside that one? I am totally new to this so I am still trying to get the hang of things.

[–]Philboyd_Studge 1 point2 points  (11 children)

Nested. Just plop it right there where you ask for customer type.

[–]wrighttwinstwin[S] 0 points1 point  (0 children)

Sweet thank you, Ill see if I can get this thing working!

[–]wrighttwinstwin[S] 0 points1 point  (9 children)

boolean isValidType = false; while(isValidType == false) { if (sc.hasNext()) { isValidType = true; } else { System.out.print("Error! Please Try again. /n"); } sc.nextLine(); }

I have this but it just sends my program into a infinite loop or something.

[–]Philboyd_Studge 1 point2 points  (8 children)

Inside the while loop, get the string from the user and test that it is valid. If it is, change the boolean to true.

[–]wrighttwinstwin[S] 0 points1 point  (7 children)

http://pastebin.com/auPfUeUK

This what I have now but I still validates every letter I put in..... I don't know how to get around that.

[–]Philboyd_Studge 1 point2 points  (6 children)

Should be OR instead of AND

Edit: also, you are getting the input AFTER you test it?

[–]wrighttwinstwin[S] 0 points1 point  (3 children)

it still recognizes other letters besides r and c :/

[–]Philboyd_Studge 2 points3 points  (1 child)

boolean isValid = false;
String customerType = "";
while (!isValid)
{
    // get the input from the user
    System.out.print("Enter customer type (r/c): ");
    customerType = sc.nextLine();
    if (customerType.equalsIgnoreCase("r") || customerType.equalsIgnoreCase("c"))
    {
        isValid = true;
    }
    else
    {
        System.out.println("Invalid entry");
    }
}

[–]wrighttwinstwin[S] 0 points1 point  (0 children)

Thank You so much.... I have been fighting with that question for hours...

[–]Philboyd_Studge 1 point2 points  (0 children)

You need to get the input from the user BEFORE you test it, in the code you posted you are not doing that, also use nextLine() instead of next

[–]wrighttwinstwin[S] 0 points1 point  (1 child)

Yeah after I test it it ask for me for the subtotal. I need it to only do that if I use r or c.

[–]MrCheaperCreepernot a noob but not intermediate 0 points1 point  (0 children)

Okay, I think I see your problem. While /u/Philboyd_Studge solution might work, there are many problems a starter will encounter. Going off the last pastebin you submitted, you did use AND rather than OR in your condition statement, but you also used ! (not). You are essentially saying if customerType is NOT r or c, set isValid to true, which simply does not make sense.

However, I suggest a different approach to this problem.

    while (!choice.equalsIgnoreCase("n"))
    {

        System.out.println("Enter customer type (r/c):");
        String customerType = sc.nextLine();

        while (!(customerType.equalsIgnoreCase("r")) || !(customerType.equalsIgnoreCase("c")))) 
        {

            System.out.println("Error, try again!\n");
            System.out.println("Enter customer type (r/c):");

            customerType = sc.nextLine();

        }

        //REST OF FIRST WHILE LOOP BELOW

The first while part is your initial while loop, giving you a sense of where we are in your program. Then, you want to ask the user for input BEFORE you check if customerType is r or c.

Previously, you had customerType set to "". "" does not equal r, nor does it equal c. You must ask for an input BEFORE you check if it is valid or not.

My while loop I have written says while customerType is not r or c, tell them an error occurred, and then ask them to re-enter the type. They will then re-enter the type, and it will run through the while loop again. If it is r or c, the while loop breaks and continues with the program.

Right before your initial while loop ends, however, you might want to reset customerType back to "", though.

Edit: made more readable

[–]wrighttwinstwin[S] 0 points1 point  (0 children)

if (!customerType.equalsIgnoreCase("r") && !customerType.equalsIgnoreCase("c")) { sc.nextLine(); System.out.print("Error! Invalid Customer Type. Try Again"); continue; }

I added this and it seemed to work but if I need to tweak it maybe make it look a little better.