ok im from C++ but no starting java this is my attempt of the password protect program which you get three try at but for some reason it dosnt compile it just gets errors

import java.util.Scanner;

public class password {
    public static void main(String args[]){
        Scanner input =  new Scanner(System.in);
        double input1, input2, input3, password;
        password = 12345;
        System.out.println("Please enter the password to procceed: ");
        input1 = input.nextDouble();

        if(input1!=password){
            System.out.println("sorry thats incorrect");
            System.out.println("Please enter your password to procceed");
            input2 = input.nextDouble();
        }else{
            System.out.println("Correct please procceed");
        }
        if(input2!=password){
            System.out.println("sorry thats incorrect");
            System.out.println("Please enter your password to procceed");
            input3 = input.nextDouble();
        }else{
            System.out.println("Correct please procceed");
        }
        if(input3!=password){
            System.out.println("sorry thats incorrect");
            System.out.println("goodbye");
        }else{
            System.out.println("correct please procced");
        }
    }
}

Recommended Answers

All 7 Replies

Welcome to the DaniWeb Java forum!
OK, rule number 1. Don't just say "it gets errors". Always post the full text of any error messages, including the line numbers. If there are no error messages but the behaviour is wrong, explain exactly what you expected vs what actually happened.
That way we won't be debugging in the daqrk.

commented: :) +14

ok well there are errors on line 18 and 25 because it says something about the varibles input2 and input3 not being delcarible yet?

Always post the full text of any error messages, including the line numbers. In this case the text of the error message is very different from your summary version.

ok thats the errors

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The local variable input2 may not have been initialized
    The local variable input3 may not have been initialized

    at password.main(password.java:18)

Exactly what is says.

The Java compiler checks all the possible paths through your code to see if you try to use a variable before you give it a value. You declared input1/2/3 but you did not initialise them at that point. input1 is OK, but if the test on lin 11 is false then you never execute the assignment on line 14, then when you get to line 18 you try to use input2. So at line 18 "The local variable input2 may not have been initialized"

In general you fix this by giving variables explicit initial values. In this case it's your logic error because if the first password attempt is OK you should not go on to test input 2 or 3.

could you give an example please im a little confused

int i;
if (something) i = 1;
System.out.println(i); // error - i may not have been initialised.

int i;
if (something) i = 1;
else i=2;
System.out.println(i); // OK, i has been given a value no matter what the if test was.

int i = 0;
if (something) i = 1;
System.out.println(i); // OK, i has been given a value no matter what the if test was.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.