Search code examples
javawhile-loopexitnumberformatexception

Program throws NumberFormatException when trying to exit program


I had to write this DateConverter program for homework this week. The program is supposed to take user input as a String in the format month/day and then it converts the input into alphanumeric (i.e. 1/21 to January 21). The program runs until the user inputs "exit" to quit. I know I have to use a while-loop to do this, but every time I try to run and exit, it throws:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Exit"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateConverter.Date(DateConverter.java:60)
at DateConverter.main(DateConverter.java:115)

The code I use to exit the program is:

while (true) { 
        System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");

        if (sc.equals("Exit") || sc.equals("exit")) {
            System.out.println("Goodbye");
            System.exit(0);

I've moved the if-statement around to a few places in order to try and clear the exception, but it won't take it. I've also googled and looked at several other questions similar to this one, and I've tried to fix the program according to those suggestions, but nothing is working. Could anyone please point me in the right direction? Here's the rest of my code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class DateConverter {    
static final ArrayList<Integer> THIRTY_DAYS = new ArrayList<>(Arrays.asList(4, 6, 9, 11));

static void Date() {
    Scanner sc = new Scanner(System.in);
    String month = null; 

    System.out.println("Welcome to the date converter!");

    while (true) { 
        System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");

        if (sc.equals("Exit") || sc.equals("exit")) {
            System.out.println("Goodbye");
            System.exit(0);
        }
        String[] input = sc.nextLine().split("/");

        int[] dateInput = new int[input.length];
        for (int i = 0; i < input.length; i++) {
           dateInput[i] = Integer.parseInt(input[i]);
    }

    if (dateInput[0] == 1) {
        month = "January";
    } else if (dateInput[0] == 2) {
        month = "February";
    } else if (dateInput[0] == 3) {
        month = "March";
    } else if (dateInput[0] == 4) {
        month = "April";
    } else if (dateInput[0] == 5) {
        month = "May";
    } else if (dateInput[0] == 6) {
        month = "June";
    } else if (dateInput[0] == 7) {
        month = "July";
    } else if (dateInput[0] == 8) {
        month = "August";
    } else if (dateInput[0] == 9) {
        month = "September";
    } else if (dateInput[0] == 10) {
        month = "October";
    } else if (dateInput[0] == 11) {
        month = "November";
    } else if (dateInput[0] == 12) {
        month = "December";
    }

    try {
        if (dateInput[0] > 12 || dateInput[0] <= 0) {
            throw new MonthException();
        } else if (dateInput[0] <= 5 && dateInput[1] <= 5 ) {
            throw new InputException();
        } else if (THIRTY_DAYS.contains(dateInput[0]) && dateInput[1] > 30) {
            throw new DayException();
        } else if (dateInput[1] > 31 || dateInput[1] <= 0) {
            throw new DayException();
        } else if (dateInput[0] == 2 && dateInput[1] > 29) {
            throw new DayException();
        }
        System.out.println("The date is " + month + " " + dateInput[1]);

    } catch (MonthException ex) {
        System.out.println(ex.getMessage());
    } catch (DayException ex) {
        System.out.println(ex.getMessage());
    } catch (InputException ex) {
        System.out.println(ex.getMessage());
    }

}       
} 

    public static void main(String[] args) {
        DateConverter.Date();
    }
}

class MonthException extends Exception {
private String month; 

MonthException() {
    super("Month Exception: Months must be between 1 and 12 inclusively.");
    this.month = month;
}

public String getMonth() {
    return month;
    }
  } 

class InputException extends Exception {
private String input;

InputException() {
    super("Input Exception: The inputed date is in the wrong format.");
    this.input = input;
}
public String getInput() {
    return input;
}
}

class DayException extends Exception {
private String day;

DayException() {
    super("Day Exception: This day is in the wrong range for the month provided.");
    this.day = day;
}

public String getDay() {
    return day;
}
}

Thank you in advance!

Edit: Here is a snippet of the updated code with the error it's throwing. The program will throw an exception now if I input a date, but exits fine. I am under the impression that all my code needs to be within the loop in order for it to continue running, but maybe I'm wrong. Could anyone point me in the right direction? And sorry if I don't understand right off the bat. I'm still kind of in the first stages of Java programming.

static void Date() {
    Scanner sc = new Scanner(System.in);
    String month = null; 

    System.out.println("Welcome to the date converter!");

    while (true) { 
        System.out.println("Enter a numeric date formatted as month/day or \"Exit \" to quit.");

        // Quits program if user enters "exit"
        String inputStr = sc.nextLine();
        if (inputStr.equalsIgnoreCase("Exit")) {
            System.out.println("Goodbye");
            System.exit(0);
        }

        String[] input = sc.nextLine().split("/");

        int[] dateInput = new int[input.length];
        for (int i = 0; i < input.length; i++) {
           dateInput[i] = Integer.parseInt(input[i]);
    }

The error:

Welcome to the date converter!
Enter a numeric date formatted as month/day or "Exit " to quit.
1/21

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateConverter.Date(DateConverter.java:47)
at DateConverter.main(DateConverter.java:104)

Solution

  • sc is a Scanner object not a String I am guessing that you want to prompt for a String so ....

        System.out.println("Enter a numeric date formatted as month/day or \"Exit\" to quit.");
    
        String inputStr = sc.nextLine();
        if (inputStr.equalsIgnoreCase("Exit")) {
            System.out.println("Goodbye");
            System.exit(0);
        }
        // more validation could be done here
        String[] input = inputStr.split("/");
    

    You could also catch the thrown exception to re-prompt the user for correct input