1

My read() methods crash each time I enter a String or Char. How would I get it to accept only integer values. When I enter an int, it works fine but when I enter a Char or String I get a repetitive "Enter the day the account opened: null" error. I have to terminate the program to stop it.

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());
        }
    }
}

// Enter the month, checking for error
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());
            }
        }
    }
}


// Enter the year, checking for error
private void readYear(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());
        }
    }
}
ncw
  • 1,635
  • 1
  • 18
  • 26
Daniel
  • 29
  • 8

3 Answers3

5

You can't stop the user from entering non-numbers; you just have to handle it when they do.

Instead of trying to read numbers, read a string, in a loop:

  • If they enter a number, parse it as an integer, use the number, and stop looping.
  • If they enter something else, print a message, and keep looping.

Something like this:

while (true) {
  String line = keyboardIn.nextLine();
  if (line.matches("\\d+")) {
    int d = Integer.parseInt(line);
    dateOpened.setDay(d);
    break;
  }

  System.out.println("Must be an integer; try again.");
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

I guess this is a homework question so should not give you the exact answer. But you can use the following:

if (input.hasNextInt()) {
    int number = input.nextInt() ; 
    System.out.println(number);
    //your code
} else {
    System.out.println("Sorry please enter a number!");
}

For more information look here:
1. How do I keep a Scanner from throwing exceptions when the wrong type is entered?
2. How to use Scanner to accept only valid int as input

Community
  • 1
  • 1
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

In this sentence "Enter the day the account opened: null", the word null is coming from your catch section, e.getMessage. If you want to keep letting users put in the number, when they key in the wrong type.

You need to initialize the variable again in try part. The demo code is as below, I have tested it already, it is working fine.

import java.util.Scanner;

public class Question01 {

    public static void main(String[] args) {

        // new Scanner(System.in)
        boolean success = false;
        Object d = null;
        while (!success) {
            try {

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

                System.out.println(d);

                success = true;
            } catch (Exception e) {

                System.out.println("Error");
                // keyboardIn.close();
            }
        }

    }
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • How do i incorporate your code above into my method. – Daniel Nov 07 '16 at 23:35
  • I need to keep the e.getMessage() in the catch block as its calling an error message from a Date class to state that the range in invalid. eg: between 1-31 and 1-12. etc. – Daniel Nov 07 '16 at 23:38
  • You can keep e.getMessage() in the code, it will not impact the existing function. For the detailed requirement of checking day or month format, I believe they are defined in your object "dateOpened", hard for me to comments more, since I cannot see it. – lpcclown Nov 08 '16 at 01:41