Alejandro Reyes
Alejandro Reyes

Reputation: 25

do-while comparison in java

Assuming the role variable has been initialized, I need to know how to do that cycle is repeated while the variable role is different from any of those three strings through a .equals() as is normally done with a single string.

do {    
    System.out.println("Input role");
    System.out.println("Administrator / Client / User");
    role=reader.stringReader();
    role=role.toLowerCase();
} while (role!="administrator" || "client" || "user");

Upvotes: 0

Views: 72

Answers (2)

c0der
c0der

Reputation: 18792

Assuming you want to check that role!="administrator" && role!="client" && role!="user use:

   //role=role.toLowerCase();
} while ( ! role.equalsIgnoreCase("administrator") &&
          ! role.equalsIgnoreCase("client") &&
          ! role.equalsIgnoreCase("user")
          );

Upvotes: 0

Keco
Keco

Reputation: 69

First create a method like this:

private static boolean isMatch(String input) {
  String[] goodInputs = new String[]{"administrator","client","users"};
  for (int i = 0; i < goodInputs.length; i++) {
    if (input.equals(goodInputs[i])) return true;
  }
  return false;
}

then you code can look like this

do {    
  System.out.println("Input role");
  System.out.println("Administrator / Client / User");
  role=reader.stringReader();
  role=role.toLowerCase();
} while (!isMatch(role));

Upvotes: 1

Related Questions