-1

I´m having trouble with the while statement. I´ve narrowed it down to my use of the "!=", because if I try it with "==", then the program works, except opposite to how I want it to. What do I use to make it so that if the user types in "v", then it won´t display the user´s input, since there´s no such thing as "!==" :)

    Scanner scannerUi = new Scanner(System.in);
    String userInput = scannerUi.nextLine();
    while (userInput != "v") {
        System.out.println(userInput);
        userInput = scannerUi.nextLine();
    }
user3207874
  • 2,815
  • 2
  • 13
  • 18

3 Answers3

0

use

while (!userInput.equals("v")) {
        System.out.println(userInput);
        userInput = scannerUi.nextLine();
    }
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
0

you should always use equals() method for compairing the strings, because equals method examine the content and == method examine the references.

pushpendra chauhan
  • 2,205
  • 3
  • 20
  • 29
0

I think that u need to compare string not on reference equality as u do it but on value equality. So, if u want to compare your current string with some other (such as "v") just use:

userInput.equals("some_string")

Using "==" u will compare strings on reference equality, and using "equals()" u will compare strings on value equality

Stas0n
  • 127
  • 1
  • 10