tags:

views:

71

answers:

3

I made a java login screen for a console application, but I need it to allow the user to input ther wrong PIN only 3 times. After the user has entered the PIN more than 3 times, the system should exit.

However, the loop which I used for the else part of the if condition does not seem to be making any changes to the program. (program wont execute the else part even once). Does anybody know what I am doing wrong?

if (userPIN.equals(a[0]))
{
  System.out.println("You have login!");
  valid=true;                       
  String b=a[2];
  Login.c=Double.parseDouble(b);
  System.out.println(c);
  obj.balance = Login.c;
  obj.MainMenu();
  System.exit(0);
}
else if(userPIN != a[0])
{
  int count=0;
  for(int i=0;i<count;i++)
  {
    System.out.println("Invalid PIN!");
    check();    
  }
}
+1  A: 

in the else part try !(userPIN.equals(a[0]))

Your else part is not checking the contents.

Sagar V
No. itz nt wrking the program is still continously displaying the login screen
Yoosuf
+3  A: 
    int count=0;
    for(int i=0;i<count;i++)

The for loop's condition is initially false, hence it will never execute its body.

Sheldon L. Cooper
+2  A: 

You have many problems in your code :

in the first if your using :

userPIN.equals(a[0])

but in the else you're using :

userPIN != a[0]

Your for loop cannot run correctly :

int count=0;
for(int i=0;i<count;i++)

Here is the correct implementation using Object-Orientation :

import java.util.Scanner;

public class PinChecker {

   // Immutable Class
   private static final class Pin {

      private String _pin;

      Pin(String pin) {
         this._pin = pin;
      }

      public String toString() {
         return _pin;
      }

      public boolean equals(Pin pin) {
         if(pin.toString().equals(_pin)) {
            return(true);
         } else {
            return(false);
         }
      }

   }

   public static final int NB_OF_TRIES = 3;

   public static void main(String[] args) {

      System.out.println("Enter your PIN :");
      Pin userPin = new Pin("FOO");

      Scanner console = new Scanner(System.in);

      boolean pinMatch = false;
      int i = 0;

      while(!pinMatch && i < NB_OF_TRIES) {

         Pin keyedPin = new Pin(console.nextLine());
         i++;

         if(userPin.equals(keyedPin)) {
            pinMatch = true;
         } else {
            System.out.println("Invalid PIN!");
         }
      }

      if(pinMatch) {
         System.out.println("OK, nb of tries :" + i);
      } else {
         System.out.println("KO, nb of tries :" + i);
      }

   }

}

You can store the keyedPin object if you need to.

Jérôme Radix
I am storing the data in a text file so I need to write the code to send the data to the keyedPin object ryt. FileInputStream fstream = new FileInputStream(".\\AccountInfo.txt"); DataInputStream in = new DataInputStream(fstream); while ((strLine = br.readLine()) != null) { String[] a = strLine.split(" "); keyedPin=a[0];}
Yoosuf