-3

Before you jump at me saying this is already answered, yes I know its answered but all questions are different,on the answered questions they use code that i have still no knowledge therefore I cannot tell what im doing wrong. Anyways getting into my error I get an error saying can't initialize variable String admin2, basically Im trying to make a password program, I know exactly what I need to change but I don't know what I have to do in order to make it work. Here is the error:

public static void main (String[] args){

     Scanner input = new Scanner(System.in);

    bacon baconObject = new bacon();

     int password;

     String admin;

     String temp; 

    System.out.println("What is your username");
      temp = input.nextLine();

    System.out.println("What is your password ");
    password = input.nextInt();

    temp = admin;

    if(temp == admin2 && password == 123){

        System.out.println("Correct signing in......");

    }else{

        System.out.println("Incorrect!");
    }


     }
 }
pagid
  • 13,559
  • 11
  • 78
  • 104
firefly23
  • 11
  • 5
  • 2
    `if(temp == admin2` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also you should post your details from error message. – Pshemo Oct 08 '15 at 23:25
  • Think about the error. What it states and means? Why you are not able to initialise it? – Balwinder Singh Oct 08 '15 at 23:26
  • Im trying to make it so that whatever the user types in is stored in temp,and if what the user typed in is equal to admin2 its gonna make a decision. – firefly23 Oct 08 '15 at 23:27
  • I can't figure out strings always theres an error but with integers I get it right... – firefly23 Oct 08 '15 at 23:28
  • if (temp.equals(admin2) – makdu Oct 08 '15 at 23:28
  • Your code gives me `admin2 cannot be resolved to a variable` and it is hard to disagree since there is no declared `admin2` in your code. Also please indent your code properly. It is nightmare to read it in current form. – Pshemo Oct 08 '15 at 23:28
  • But what is `admin2`? – Paul Boddington Oct 08 '15 at 23:28
  • admin2 is basically the test for the user,If the user typed in admin2 then that is the correct "username" – firefly23 Oct 08 '15 at 23:30
  • Voting to close as yet another java String comparison question. – Matt Clark Oct 08 '15 at 23:32
  • Ok thanks it worked,and it is another String question but its different,and on the questions there were weird coide that i have yet not achieved sooo... – firefly23 Oct 08 '15 at 23:34

1 Answers1

1

If you want to check if the text referenced by the temp variable is "admin2", then you would put if (temp.equals("admin2")) instead of if (temp == admin2).

The reason for this is that in the second example (and your version), it is referencing "admin2" as a variable, but you want to use it as a String, which is achieved by putting quotes around it.

And the reason that you use .equals instead of == is because == compares the actual object that the variable is referenced to, while .equals compares the content of the object the variable is referencing.

Shapi
  • 5,493
  • 4
  • 28
  • 39