3
import java.util.Scanner;

public class OrigClass {
public static void main (String[] args){
Scanner ScanObj = new Scanner(System.in);
System.out.println("Set Password: ");
String SetPass = ScanObj.nextLine();
System.out.println("Guess Password: ");
String GuessPass = ScanObj.nextLine();

while (GuessPass != SetPass){
   System.out.println("Incorrect. Try Again: ");
   GuessPass = ScanObj.nextLine();  }}}

So as you can see I'm trying to make a very simple program that lets the user enter a password then re-enter it. I'm a little confused as to why this isn't working. I've tried everything I can think off and have read up on other similar problems on here, but can't find anything...

Sorry if this is a bit basic, but it's annoying me that I can't get this to work :(

Sam
  • 97
  • 2
  • 4
  • 11

3 Answers3

3

You should compare the string with String.equals

so your code should look like this :

while (!GuessPass.equals(SetPass)){

and as mentionned in the comments I'd change the name of your variables to match the Java standards (camelCase for local variables and methods) , so to sum up :

while (!guessPass.equals(setPass)){
Pacane
  • 20,273
  • 18
  • 60
  • 97
2

When comparing strings use String.equals("some string").

while(!guessPass.equals(setPass)){
//do something
}

I included changes to your variable names. Standard for variables is first word lowercase and all subsequent words uppercase (first letters). This will help you and your peers understand your code. Hope this helps.

PandaBearSoup
  • 699
  • 3
  • 9
  • 20
0

Here is the fix for you. You need to use dot equal when comparing strings.

        public static void main(String[] args) {
            Scanner ScanObj = new Scanner(System.in);
            System.out.println("Set Password: ");
            String SetPass = ScanObj.nextLine();
            System.out.println("Guess Password: ");
            String GuessPass = ScanObj.nextLine();
            // the fix
            while (!GuessPass.equals(SetPass)){
               System.out.println("Incorrect. Try Again: ");
               GuessPass = ScanObj.nextLine();  }}
}
grepit
  • 21,260
  • 6
  • 105
  • 81