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;
}
}