Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PIN Code program in Java (optimization)

843785Jan 23 2009 — edited Jan 24 2009
package atm;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class PinCheck {

    public String enterPin() {
        String inputPinNo2 = "0000";
        InputStreamReader keyInput = new InputStreamReader(System.in);
        BufferedReader buffInput = new BufferedReader(keyInput);
        System.out.print("Enter pin: ");
        try {
            inputPinNo2 = buffInput.readLine();
        } catch (IOException ex) {
        Logger.getLogger(PinCheck.class.getName()).log(Level.SEVERE, null, ex);
        }
        return inputPinNo2;
    }

    public static void main(String[] args) {
        int pinRetries = 0;
        String pinNo = "0907";
        String inputPinNo = "0907";
        do
        {
            PinCheck enterPinNo = new PinCheck();
            inputPinNo = enterPinNo.enterPin();
            if (!(pinNo.equals(inputPinNo)))
            {
                System.out.println("Wrong Pin");
                pinRetries++;
            }
            else
            {
                System.out.println("Access granted");
                pinRetries = 0;
                break;
            }
        } while (pinRetries < 3);
        if (pinRetries == 3)
        {
            System.out.println("Account blocked");
        }
        else
        {
            System.out.println("Welcome!");
        }
    }
}
There's my code for the PIN. It asks the user for a PIN. If it's correct, it displays "Welcome". If it's not, the same question is asked 3 times until the user gets the correct PIN, or its account gets blocked.

It's working, but I think it maybe too long or too inefficient. I'm new to Java programming. Can anybody comment and/or optimize this code? Please? Thank you very much.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 21 2009
Added on Jan 23 2009
14 comments
1,335 views