-1

m trying to write a super simple piece of code which asks "are we nearly there yet" if the user inputs yeah they get the answer "finally" if the user says anything other than yeah the computer keeps asking are we nearly there yet?

for some reason when I run the code below even if I type "yeah" after the first prompt the computer just keeps printing are we nearly there yet? and can't get through the loop.

    import java.util.Scanner;

    class FirstAttempt {

    public static void main(String args[]) {
    
    Scanner s= new Scanner(System.in);
    String a ="  ";
    
    System.out.println("are we nearly there yet?");
    a  = s.next();
    
  
    
    while ( a != "yeah" & a != "Yeah")
        
       {System.out.println("are we nearly there yet?");
           s = new Scanner(System.in);
        a = s.nextLine(); }
        
        System.out.println("finally");
      
      } }
John
  • 21
  • 5

4 Answers4

2

The issue is just in the way you wrote the condition in the while.

The application doesn't go in the loop, because your condition is always false. Basically, that's because you are not comparing the values of the String objects, but the values of their instances.

In Java, you must compare one String object with another by using equals and not with operators != or ==

Plus, I suggest that you use &&, instead of &. The first one is short-circuiting, which means that the first condition is evaluated, and only in case it's true the second one is also evaluated.

However, there is a more efficient way to do that. Look at this:

while ( !a.equalsIgnoreCase("yeah")) )
Marco Tizzano
  • 1,726
  • 1
  • 11
  • 18
1

To test if strings are equal you need to use equals:

while ( !(a.equals("yeah") || a.equals("Yeah")) )

& is a bitwise operation -- you probable want the logical operations && and ||.

Also, no need to create a new Scanner inside the loop. Just keep using the one from the top of the main method.

Erik
  • 578
  • 2
  • 9
0

The & operator, otherwise known as 'bitwise and' will evaluate all conditions regardless of whether the first condition is true or false. So while the input "yeah" makes the first condition false and thus loop-breaking, the second condition is still evaluated to true and keeps the loop going. You should use &&

DLynch
  • 166
  • 1
  • 8
  • 2
    `&` will produce the *exact same* result as `&&`. The only difference is that `&&` uses short-circuit logic, whereas `&` does not. In either case, `a` can't be `"yeah"` and `"Yeah"` at the same time, and it also can't be a string literal. – Charlie Armstrong Mar 10 '21 at 22:09
0

You have to change
while ( a != "yeah" & a != "Yeah") to while ( a != "yeah" || a != "Yeah") as it can't be both at the same time

  • 1
    This will not work. `a` is only assigned to input from the scanner, it cannot equal a string literal. – Charlie Armstrong Mar 10 '21 at 22:07
  • yes that is correct no matter whether I use || or && it still doesn't work therefore it must be a string problem. do you have any suggestion to make the string variables the same. or know anyway to compare the string value a to a literal – John Mar 10 '21 at 22:15
  • 1
    @John Refer to the canonical: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java and Erik's answer. You need to use `a.equals("yeah")` because `equals()` actually checks the contents of the strings to check if they are equal, whereas `==` only checks to see if they are the same string object. – Charlie Armstrong Mar 10 '21 at 22:21
  • as already commented, String comparison made with operators != or == won't work as variable 'a' is String object, not a literal, so you must use 'equals()' function. – Marco Tizzano Mar 10 '21 at 22:47
  • `a` can be different from one value _and_ be different from another value at the same time. – Erik Mar 10 '21 at 22:53