Search code examples
javaloopsfor-loopwhile-loop

How to well adjust a loop in Java?


I am currently writing a little program to ask a PinCode to a user and return ":)" if the Pin is good or ":(" if the Pin is wrong. My code is made of one java file and one text file.

This is the code :

import java.io.*;
import java.util.*;

public class Main {

static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
   boolean err = false;
   try {
      Scanner scanner = new Scanner(dataFile);
      String line;
      while (scanner.hasNext()) {
         line = scanner.nextLine();
         try {
            data.add(Integer.parseInt(line));
         } catch (NumberFormatException e) {
            e.printStackTrace();
            err = true;
         }
      }
      scanner.close();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
      err = true;
   }

   return err;
}


public static void main(String[] args) {

    System.out.println("-----------------------");
    System.out.println("MY APP");
    System.out.println("-----------------------");
    Console console = System.console();
    int pinSize = 0;
    int nbTry = 0;

    do{
      do{
    char passwordArray[] = console.readPassword("Enter pin: ");
    pinSize = passwordArray.length;

    if(pinSize != 4){
            System.out.println("Pin must be 4 digits");
        } else {
            System.out.println("Checking...");
        }


   ArrayList<Integer> pins = new ArrayList<Integer>();
   readPinsData(new File("bdd.txt"), pins);
   //System.out.println(pins);
   //System.out.println(passwordArray);


   String[] thePins = new String[pins.size()];
   for (int i = 0; i < thePins.length; i++) {
    thePins[i] = pins.get(i).toString();
}

   String passEntered = String.valueOf(passwordArray);





   for(int i = 0 ; i < thePins.length ; i++){
      if(passEntered.equals(thePins[i]) && pinSize == 4){
          System.out.println(":)");
        } else if(!passEntered.equals(thePins[i]) && pinSize == 4){
            nbTry++;
        }

    }   

  }while(nbTry < 3);
   }while(pinSize != 4);

}
}

This is bdd.txt where all the good Pins are stored :

1111
2222
3333
4444
5555
6666
7777
8888
9999

Actually my problem is to limit the number of try to 3 tries. I need to explain:

--> the user has to enter a pin

--> either he enters a good 4 digits pin and it prints ":)" (and the app is done)

--> either he enters a wrong 4 digits pin and it prints ":(" and the nbTry must be ++. In this case he has only 2 tries left

--> he also can enter a 1-digit pin or 2-digits pin or 3-digits pin ... and in this case nbTry is not affected, he just have to re-enter a 4 digits pin.

I can not find out how to do with the nbTry left part.. Any ideas ?


Solution

  • Do you want him to be able to enter a 4 digit pin only or do you want him to be able to enter any length of pin?

    Edit:

    Reading your main I saw that you have two 'do...while`. If you change the order of them it should work. I can't test it atm because I'm on mobile but try it like this:

    do {
      do {
          ....
      } while (pinSize != 4);
    } while (nbTry < 3);
    

    Edit2:

    boolean loginCorrect = false;
    for (int i = 0; i < thePins.length; i++) {
       if (passEntered.equals(thePins[i]) && pinSize == 4) {
          System.out.println(":)");
          loginCorrect = true;
          break;
       } else if (!passEntered.equals(thePins[i]) && pinSize == 4) {
          System.out.println(":(");
       }
    
    }
    if(!loginCorrect && pinSize == 4){
       nbTry++;
    }
    

    Hope you got it as its hard to type on mobile.

    The full main code: public static void main(String[] args) {

        System.out.println("-----------------------");
        System.out.println("MY APP");
        System.out.println("-----------------------");
        Console console = System.console();
        int pinSize = 0;
        int nbTry = 0;
        boolean authenticated = false;
    
        do {
            do {
                char passwordArray[] = console.readPassword("Enter pin: ");
                pinSize = passwordArray.length();
    
                if (pinSize != 4) {
                    System.out.println("Pin must be 4 digits");
                } else {
                    System.out.println("Checking...");
                }
    
                ArrayList<Integer> pins = new ArrayList<Integer>();
                readPinsData(new File("bdd.txt"), pins);
                // System.out.println(pins);
                // System.out.println(passwordArray);
    
                String[] thePins = new String[pins.size()];
                for (int i = 0; i < thePins.length; i++) {
                    thePins[i] = pins.get(i).toString();
                }
    
                String passEntered = String.valueOf(passwordArray);
    
                for (int i = 0; i < thePins.length; i++) {
                    if (passEntered.equals(thePins[i]) && pinSize == 4) {
                        System.out.println(":)");
                        authenticated = true;
                        break;
                    }
                }
    
            } while (pinSize != 4);
            if (!authenticated && pinSize == 4) {
                System.out.println(":(");
                nbTry++;
            }
        } while (nbTry < 3 && !authenticated);
    }