-2

I'm trying to make a log in screen which has 10 buttons 0-10 and only accepts 4 digits. I have stored some variables called PIN0-PIN03 which holds a int value. I want the user to enter 4 digit pin and if it matches the values stored in the variables PIN0-PIN03 a name will appear.

I have done the button and everything I just need help with this ENTER button

the code I used for the ENTER button is:

int pin = 0000;
int PIN0 = 1234; // if user enters this value the program will start executing the if statments.
int PIN1 = 2345;
int PIN2 = 3456;
int PIN3 = 4567;\\

if (pin == PIN0){

System.out.println("Hi Muhannad");
if (pin == PIN0){ // if user enters PIN0 it will display the below message

System.out.println("Hi Muhannad");
}

if( pin == PIN1){
System.out.println("Hi Mo");
}

if( pin == PIN2){
System.out.println("Hi Mahir");
}

if( pin == PIN3){


System.out.println("Hi Gawi");
}

if(pin != PIN0 || pin == PIN1 || pin != PIN2 || pin != PIN3){
System.out.println("Incorrect pin try again!");  // if user enters anything that doesn't match any of the variables values the "Incorrect pin try again" will apear. 
}

I'm I doing this right?

please help this is a project I started in my own time to create an app similar to banking apps.

  • watched several videos but I couldn't find the right on – MOHAMED AHMED Oct 01 '14 at 00:11
  • 1
    Have a look at [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [How to Write an Action Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) and may be even [How to Use Actions](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) – MadProgrammer Oct 01 '14 at 00:12
  • Possible [duplicate](http://stackoverflow.com/q/10099686/230513). – trashgod Oct 01 '14 at 00:30

2 Answers2

1

Try adding an ActionListener to the "submit pin" button.

button.addActionListener(new ActionListener() {
    //This will be called whenever you click the button.
    public void actionPerformed(ActionEvent e) {
       //open GUI here:
       //Maybe something like:
       frame.setVisible(true);

    }
});

Simplify:

if(pin != PIN1 || pin != PIN2 || pin != PIN3){
  System.out.println("Incorrect pin try again!");
}else{
if( pin == PIN1){
    System.out.println("Hi Mo");
}

if( pin == PIN2){
    System.out.println("Hi Mahir");
}

if( pin == PIN3){
 System.out.println("Hi Gawi");
}
}

try now.

Petro
  • 3,484
  • 3
  • 32
  • 59
  • i did something like if (pin != PIN1){ System.out.println("Incorrect pin try again!"); if( pin == PIN1){ System.out.println("Hi Mo"); } for the submit button... but i get the sys.out.println("....") appearing 3 times do i need to use a break? and how – MOHAMED AHMED Oct 01 '14 at 00:31
  • please post code in answer so I can see what your doing – Petro Oct 01 '14 at 00:38
  • just did have look plz :) – MOHAMED AHMED Oct 01 '14 at 00:49
  • did this help? this would be an issue if this was a REAL ATM, however I'm guessing this is a school project, so hacking by typing random pins probably isn't part of the project. – Petro Oct 01 '14 at 01:56
  • Yes thank you very much that works... and nope not REAL atm lol or school project just some practice I'm new too Java and I'm trying to improve thats all :)..... – MOHAMED AHMED Oct 01 '14 at 02:05
  • one question why do I still get inccorect pin! try again message when I enter the correct pin? – MOHAMED AHMED Oct 01 '14 at 02:06
  • oh sorry thats because they arn't equal to every PIN i'll fix – Petro Oct 01 '14 at 05:06
  • what do you suggest I do...... As you can see am still stuck! lol appreciate the help – MOHAMED AHMED Oct 05 '14 at 17:36
1

you can find out how many digits were entered in jTextField, check it via if condition, if the number of digits entered == 4 then notify an EventListener. Make an inner class, Implement the EventListener Interface and start the new GUI in the interface method

Makaveli
  • 101
  • 10