-1

Why is my first while loop checking the validity of input from the user but the other two while loops for month and year are just being skipped over (ignored)? I'm trying to get a user to enter a date correctly and not have them enter impossible values.

// A read() method to read in the account details from the user
boolean success = false;
public void read()
{
    Scanner keyboardIn = new Scanner(System.in);        
    System.out.println("ENTER ACCOUNT DETAILS: ");
    System.out.print("User Title: ");
    String title = keyboardIn.nextLine();
    name.setTitle(title);
    System.out.print("User First name: ");
    String firstname = keyboardIn.nextLine();
    name.setFirstName(firstname);
    System.out.print("User Second name: ");
    String secondname = keyboardIn.nextLine();
    name.setSurname(secondname);
    System.out.print("Account Address: ");
    address = keyboardIn.nextLine();
    // To make sure day is entered correctly (1 - 31)
    while(!success)
    {
        try
        {
            System.out.print("Enter the day the account opened: ");
            int d = keyboardIn.nextInt();
            dateOpened.setDay(d);
            success = true;
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

    // To make sure month is entered correctly (1 - 12)  
    while(!success)
    {   
        try
        {
            System.out.print("Enter the month the account opened: ");
            int m = keyboardIn.nextInt();
            dateOpened.setMonth(m);
            success = true;
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

     // To make sure year is entered correctly (< 1900 not permitted)
    while(!success)
    {   
        try
        {
            System.out.print("Enter the year the account opened: ");
            int y = keyboardIn.nextInt();
            dateOpened.setYear(y);
            success = true;
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

    System.out.print("Enter the initial balance: ");
    balance = keyboardIn.nextDouble();
    System.out.print("Enter the overdraft amount: ");
    overdraftAmount = keyboardIn.nextDouble();
    System.out.println("Account number: " + accountNo);
    System.out.println();
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Daniel
  • 29
  • 8

3 Answers3

1

Since the looping condition of your first while loop only terminates when success is true, success will be true for the other two while loops and they will never run.

I would not recommend using "success" as a looping condition. You can do something like

System.out.print("Enter the day the account opened: ");
int d = keyboardIn.nextInt();

while(d < 1 || d>31){

System.out.print("Enter the day the account opened: ");
d = keyboardIn.nextInt();

}

//set d as day in object
Solace
  • 2,161
  • 1
  • 16
  • 33
  • // This is my output, the Date field is coming up as 0/0/0 for some reason. The validation works ok now though. I changed it to what your suggestion was. – Daniel Nov 06 '16 at 22:35
  • Name: Mr Joe Bloggs Address: 1 Main Street Date opened: 0/0/0 Balance: €100.0 Overdraft Available: €200.0 – Daniel Nov 06 '16 at 22:36
  • could you please update the source code in the question – Solace Nov 06 '16 at 22:37
0

It could be for 2 reasons, the first one is that the data entered by the user is not an integer, which throws an InputMismatchException exception or in the line with dateOpened.setDay(d) failed, that could happens if dateOpened is null or if the method setDay fails which won't the success variable set to true.

Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
0

After your first while loop you dont reset the loop condition "success".

One way to solve this would be to just reset it after each loop

on the other hand implementing it in smaller methods and making the condition variable local to that method would solve that too.

public void read()
{
    Scanner keyboardIn = new Scanner(System.in);        
    System.out.println("ENTER ACCOUNT DETAILS: ");
    System.out.print("User Title: ");
    String title = keyboardIn.nextLine();
    name.setTitle(title);
    System.out.print("User First name: ");
    String firstname = keyboardIn.nextLine();
    name.setFirstName(firstname);
    System.out.print("User Second name: ");
    String secondname = keyboardIn.nextLine();
    name.setSurname(secondname);
    System.out.print("Account Address: ");
    address = keyboardIn.nextLine();
    // To make sure day is entered correctly (1 - 31)
    readDay(keyboardIn);

    // To make sure month is entered correctly (1 - 12)  
    readMonth(keyboardIn);

     // To make sure year is entered correctly (< 1900 not permitted)
    readYear(keyboardIn);

    System.out.print("Enter the initial balance: ");
    balance = keyboardIn.nextDouble();
    System.out.print("Enter the overdraft amount: ");
    overdraftAmount = keyboardIn.nextDouble();
    System.out.println("Account number: " + accountNo);
    System.out.println();

}

private void readDay(Scanner keyboardIn){
    boolean success = false;
    while(!success)
    {
        try
        {
            System.out.print("Enter the day the account opened: ");
            int d = keyboardIn.nextInt();
            dateOpened.setDay(d);
            success = true;
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
            keyboardIn.next();
        }
    }
}
private void readMonth(Scanner keyboardIn){
    boolean success = false;
    while(!success)
    {   
        try
        {
            System.out.print("Enter the month the account opened: ");
            int m = keyboardIn.nextInt();
            dateOpened.setMonth(m);
            success = true;
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
            keyboardIn.next();
        }
    }
}
private void readMonth(Scanner keyboardIn){
    boolean success = false;
    while(!success)
    {   
        try
        {
            System.out.print("Enter the year the account opened: ");
            int y = keyboardIn.nextInt();
            dateOpened.setYear(y);
            success = true;
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
            keyboardIn.next();
        }
    }
}
matte
  • 1
  • 2
  • Ok this works perfectly for me so thank you. What if someone was to enter a non integer value though like 6.5or 12.5 or say a character like A-Z. how can i combat a user from doing that. – Daniel Nov 06 '16 at 22:47
  • Could i get help with the problem of disallowing String input please as my code crashes each time i enter a letter. – Daniel Nov 07 '16 at 21:40
  • As it is stated in [link](http://stackoverflow.com/a/3572233/1964912) you have to call keyboardIn.next(); to consume the values held by the scanner in the catch clause – matte Nov 10 '16 at 09:37