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;
}
}
!=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
Eg
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"))
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;
}
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.