1

For some reason, when I have multiple correct strings, the statement keeps repeating

    do {
    System.out.println("Enter Service Code");
    Scanner a = new Scanner(System.in);
    serviceCode = a.nextLine();
} while (!serviceCode.equals("ORB1") || !serviceCode.equals("ORBH") || 
         !serviceCode.equals("ISS5") || !serviceCode.equals("ILLOYDS") || 
         !serviceCode.equals("DLAB") || !serviceCode.equals("LEOM7") || 
         !serviceCode.equals("MOON2"));

However, when there's just one string that the code checks against. The do while statement works fine and will stop looping when the correct input is entered

    do {
    System.out.println("Enter Service Code");
    Scanner a = new Scanner(System.in);
    serviceCode = a.nextLine();
} while (!serviceCode.equals("ORB1"));
4
  • Put System.out.println("-"+serviceCode) after you call a.nextLine() and you will know why^^ Commented Sep 19, 2017 at 6:45
  • 1
    take a look at your condition. Let's say you input "ORB1". Then, !serviceCode.equals("ORB1") = False, because they're equal. But, what about the others? They keep returning True, because they're not equal. Commented Sep 19, 2017 at 6:45
  • bcoz all those other ors will be true Commented Sep 19, 2017 at 6:45
  • Please don't instanciate a Scanner in a loop !!! Commented Sep 19, 2017 at 7:02

3 Answers 3

1

If you enter "ORB1", "!serviceCode.equals("ORB1")" will return false but the others will return true; and you are using the "OR" operator. So, this sentence :

!serviceCode.equals("ORB1") || !serviceCode.equals("ORBH") || 
     !serviceCode.equals("ISS5") || !serviceCode.equals("ILLOYDS") || 
     !serviceCode.equals("DLAB") || !serviceCode.equals("LEOM7") || 
     !serviceCode.equals("MOON2")

will always be true. You need to use the "AND" operator

!serviceCode.equals("ORB1") && !serviceCode.equals("ORBH") && 
     !serviceCode.equals("ISS5") && !serviceCode.equals("ILLOYDS") &&
     !serviceCode.equals("DLAB") && !serviceCode.equals("LEOM7") && 
     !serviceCode.equals("MOON2")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Don't know how I looked over that one
1

Your comparison can never return false. it's either A or B.

so, if you were to say:

if ( !A OR !B ){ 

--> Input = A => true (because !B returns true) --> Input = B => true (because !A returns true) --> Input = C => true (because !A returns true)

Change your OR (||) by AND (&&)

Also: declare and instantiate your Scanner before your loop.

Comments

1

A better approach would be create a Listof string which includes the valid codes and check if that list contains the provided user input.

List<String> validServiceCodes = Arrays.asList("ORB1", "ORBH", "ISS5", "ILLOYDS", "DLAB", "LEOM7", "MOON2" );

do {
    System.out.println("Enter Service Code");
    Scanner a = new Scanner(System.in);
    serviceCode = a.nextLine();
} while (!validCodes.contains(validServiceCodes));

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.