Nikos
Nikos

Reputation: 11

Is there a way to control the string the user gives with do while in Java?

The user has to give just two strings in my case "AM" or "PM". I have already write the do while(); loop to do that. But when inside while(); I have both expressions the loop becomes infinite. When I have just one no matter which one, the program run perfect. Thanks in advance for any solution!

import java.util.Scanner;

class Time{
  public static void main(String[] args){

    int hours, minutes, sec;
    String period, x, y;


    x = "AM";
    y = "PM";

    Scanner reader = new Scanner(System.in);

    System.out.println("");
    System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------");
    System.out.println("Give the values you will be asked. Those values form the time give them in such a way that they would appear in a digital clock. (ex.: 2pm = 14)");
    System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------");
    System.out.println("");

    System.out.println("..............................................");
    do{
        System.out.print("Chooce the current time period (AM or PM) : ");
        period = reader.nextLine();

        if(!period.equals(x) || !period.equals(y)){
            System.out.println("Your input should be AM or PM");
        }
    }while(!period.equals(x) || !period.equals(y));

Upvotes: 0

Views: 94

Answers (4)

Soutzikevich
Soutzikevich

Reputation: 1031

The problem with your code, is that the terminating condition you give to your while loop, is:

if user input is not-equal to X OR Y, then keep spinning (don't exit loop).

That condition should be changed to:

while(!period.equals(x) && !period.equals(y));

In a while() loop, the condition for that loop is checked first. So, if the condition you enter for that loop is true, then the code inside it, will be executed. If the condition is false, then the code will not be executed.

In a do-while() loop, the code will be executed first and the condition will be checked after that code is executed AT LEAST ONCE.

Upvotes: 0

Makoto
Makoto

Reputation: 106480

My IDE told me that this expression always returns "true":

!period.equals(x) || !period.equals(y)

Let's break down why. Using DeMorgan's Laws to simplify the logic, we arrive at this expression.

!(period.equals(x) && period.equals(y))

So even if period were to equal "AM", it would not equal "PM" and would fail the expression, thus making the statement always true.

If you flip your expression such that you use && instead:

!period.equals(x) && !period.equals(y)

DeMorgan's laws state:

!(period.equals(x) || period.equals(y))

...which is more of what we want. It's either not "AM" or it's not "PM".

Upvotes: 2

Stalemate Of Tuning
Stalemate Of Tuning

Reputation: 727

Your condition says

while(!period.equals(x) || !period.equals(y));

So that means while period is NOT "AM" OR "PM" we should continue.

Let's say we set period to "AM". This is desired, but according to your logic we must continue, even though period is "AM", it's also NOT "PM". See the issue?

Try this instead

while(!period.equals(x) && !period.equals(y));

Upvotes: 6

Matteo Tomai
Matteo Tomai

Reputation: 174

change while(!period.equals(x) || !period.equals(y));

In : while(!period.equals(x) && !period.equals(y));

You have to put the condition in AND otherwise it is always evaluated as true

is "AM" not equals to "AM" false is "AM" not equals to "PM" true

true or false = true | (1+0 = 1)

Upvotes: 5

Related Questions