0

Am currently working on a question for my Java class, I'm doing do-while loops and I'm having trouble when my while condition has to do with a user inputted String. The code compiles but no matter what I enter it fails the while condition and does the loop again. This even occurred when I hardcoded the value of unit.

I tried looking up the solution online but every example I can find uses a user inputted int value instead of a String


public class unit {
   public static void main (String[] args) {
      String prompt = "Please enter your preferred unit of mass (kg, lb, g or oz): ";
      System.out.println(preferredUnit(prompt));
   }
   
   public static String preferredUnit(String prompt) {
      Scanner sc = new Scanner(System.in);
      String unit = "";
            
      do{
         if (!unit.equals("")) {
            System.out.println("Sorry but " + unit + " is not a valid unit type");
         }
         System.out.println(prompt);
         unit = sc.nextLine();
      
      }  while(!unit.equals("kg") || !unit.equals("lb") || !unit.equals("g") || !unit.equals("oz") );
      
      return "Unit of mass: " + unit; 
   }
} 
3
  • The Problem in your while loop condition. Commented May 4, 2021 at 4:24
  • 1
    It always return true. Because when you enter anything it will check !unit.equals("kg") return true. so It's OR operator. One of them is true mean While run again Commented May 4, 2021 at 4:27
  • 1
    Use && Operator Instead of ||. Commented May 4, 2021 at 4:29

3 Answers 3

1
!=A || !=B

is not what you're looking for. If I enter A, it's not B, so it fails. If I enter B, it's not A, so it fails. Instead you're looking for !=A && !=B

E.g.

while(!unit.equals("kg") || !unit.equals("lb") || !unit.equals("g") || !unit.equals("oz"))

Should be

while(!unit.equals("kg") && !unit.equals("lb") && !unit.equals("g") && !unit.equals("oz"))
Sign up to request clarification or add additional context in comments.

Comments

1

Two possible Fixed of your problem.

One Way:

 public static void main (String[] args) {
      String prompt = "Please enter your preferred unit of mass (kg, lb, g or oz): ";
      System.out.println(preferredUnit(prompt));
   }
   
   public static String preferredUnit(String prompt) {
      Scanner sc = new Scanner(System.in);
      String unit = "";
            
      do{
         if (!unit.equals("")) {
            System.out.println("Sorry but " + unit + " is not a valid unit type");
         }
         System.out.println(prompt);
         unit = sc.nextLine();
      
      }  while(!unit.equals("kg") && !unit.equals("lb") && !unit.equals("g") && !unit.equals("oz") );
      
      return "Unit of mass: " + unit; 
   }

Second way:

public static void main (String[] args) {
      String prompt = "Please enter your preferred unit of mass (kg, lb, g or oz): ";
      System.out.println(preferredUnit(prompt));
   }
   
   public static String preferredUnit(String prompt) {
      Scanner sc = new Scanner(System.in);
      String unit = "";
            
      do{
         if (!unit.equals("")) {
            System.out.println("Sorry but " + unit + " is not a valid unit type");
         }
         System.out.println(prompt);
         unit = sc.nextLine();
      
      }  while(!(unit.equals("kg") || unit.equals("lb") || unit.equals("g") || unit.equals("oz")) );
      
      return "Unit of mass: " + unit; 
   }

Comments

1

The do-while loop says: do something while a condition is true. Your condition is:

   !unit.equals("kg") || !unit.equals("lb") || !unit.equals("g") || !unit.equals("oz")

and it must be false to exit from the do-while loop.
If you enter kg, the first condition (!unit.equals("kg")) is false and it is what you want, but unfortunately, because the next condition (!unit.equals("lb")) is true and it is in OR (||), you remain in the do-while loop whatever is your input.
You need to change the condition using AND (&&):

   !unit.equals("kg") && !unit.equals("lb") && !unit.equals("g") && !unit.equals("oz")

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.