Hello,

I'm new in Java and this is my first post here.

I was doing my homework but faced the following error:

c:\java\my box\Login.java:25: variable p might not have been initialized
if (p == pass) {
^
1 error

You can see the main code bellow:

import java.util.Scanner;
public class Login {
    public static void main (String[] args) {
             String user = "admin";
             int pass = 123;
             int p;
             String u;
    	Scanner s=new Scanner(System.in);
            System.out.println ("Enter Username:");
    	    u=s.nextLine();
    	 if (u == user) {
    	  	System.out.println ("Enter Password:");
    	 	p=s.nextInt();
    	 } else {
    	  	System.out.println ("Invalid User");
    	 }
      	  	if (p == pass) {
    	        System.out.println (" ");
    	 	System.out.println ("Welcome to admin area.");
    	        System.out.println (" ");
    	    } else {
    	 	System.out.println ("Incorrect Password");
    	    }
    }

}

What's wrong with my code? Am I missing something?


Any helps will be greatly appreciated

Edited 4 Years Ago by R32@: n/a

If you don't pass on the first "if" statement then "p" will have no value making it uninitialized

Edited 4 Years Ago by zeroliken: n/a

you need to give your variable p a default value, either where you say:

int p;

you should say

int p = -1;

or, in your if-else statement:

if (u == user) {
    	  	System.out.println ("Enter Password:");
    	 	p=s.nextInt();
    	 } else {
    	  	System.out.println ("Invalid User");
    	 }

you'll need something like:

if (u == user) {
    	  	System.out.println ("Enter Password:");
    	 	p=s.nextInt();
    	 } else {
    	  	System.out.println ("Invalid User");
// add default here:
p = -1;
    	 }

or, you need to put the rest of your code:

if (p == pass) {
    	        System.out.println (" ");
    	 		System.out.println ("Welcome to admin area. Please choose an option");
    	        System.out.println (" ");
    	    } else {
    	 	    System.out.println ("Incorrect Password");
    	 	}

inside the if block of your first if statement. that way, you won't try to compare the value of p with another one, before you've actually assign a value to p.

Edited 4 Years Ago by stultuske: n/a

(u == user)

As gets posted here about once a week on average... don't try to compare Strings with ==, use .equals instead. Google for details.

Thank you everybody.
I tried your suggesstions and the error has gone away and program has been compiled successfully. but when I execute my program, after entering the username, It returns Invalid Username message.

Please check the following code:

import java.util.Scanner;
public class Login {
    public static void main (String[] args) {
             String user = "admin";
             int pass = 123;
             int p;
             String u;
    	Scanner s=new Scanner(System.in);
            System.out.println ("Enter Username:");
    	 u=s.nextLine();
    	 if (u == user) {
    	  	System.out.println ("Enter Password:");
    	 	p=s.nextInt();
    	    if (p == pass) {
    	        System.out.println (" ");
    	        System.out.println ("Welcome to admin area. Please choose an option");
    	        System.out.println (" ");
                     }
                 } else {
    	        System.out.println ("Incorrect Password");
         	    }
         } else {
    	        System.out.println ("Invalid User");
         }
    }

}

Thank you everybody.
I tried your suggesstions and the error has gone away and program has been compiled successfully. but when I execute my program, after entering the username, It returns Invalid Username message.

Please check the following code:

import java.util.Scanner;
public class Login {
    public static void main (String[] args) {
             String user = "admin";
             int pass = 123;
             int p;
             String u;
    	Scanner s=new Scanner(System.in);
            System.out.println ("Enter Username:");
    	 u=s.nextLine();
    	 if (u == user) {
    	  	System.out.println ("Enter Password:");
    	 	p=s.nextInt();
    	    if (p == pass) {
    	        System.out.println (" ");
    	        System.out.println ("Welcome to admin area. Please choose an option");
    	        System.out.println (" ");
                     }
                 } else {
    	        System.out.println ("Incorrect Password");
         	    }
         } else {
    	        System.out.println ("Invalid User");
         }
    }

}

check JamesCherrill's post above

Thanks a lot
I used .equals instead of == and it works like a charm :)

This question has already been answered. Start a new discussion instead.