Pejman Tehrani
Pejman Tehrani

Reputation: 3

Date/month validation

here is a description on what I'm trying to do:

First, the program will print "Enter month>," and then the user must enter a number. If the entered number is not in the range of 1 to 12 as the following error message is printed "Improper month.", And the program then ends. If the entered month is correct, the program will print "Enter the day of month>," and then the user must enter a to-speech. If the number does not correspond to a day in the previous input the month, the following error message is printed "Wrong day of the month.", And the program then ends. If both the input month number and the entered day of the month is correct, then the program should print "The correct date.", And then terminated.

But I get this error below and I can't figure out why

Exception in thread "main" java.lang.NumberFormatException: For input string: "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\ ][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q∞\E]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at upp.main(upp.java:10)


import java.util.Scanner;
public class upp {
public static void main(String[] args) {

    int[] numberOfDaysEachMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    System.out.println("Enter month > ");
    Scanner month = new Scanner(System.in);
    int userInputMonth = Integer.parseInt(month.toString());
    if (userInputMonth > 0 && userInputMonth < 13) {
        System.out.println("Enter the day of month > ");
        Scanner day = new Scanner(System.in);
        int userInputDay = Integer.parseInt(day.toString());
        if (userInputDay > 0 && userInputDay < numberOfDaysEachMonth[userInputMonth - 1]) {
            System.out.println("Correct date.");
        } else {
            System.out.println("Wrong date.");
        }
    } else {
        System.out.println("Wrong month.");
    }
}
}

Upvotes: 0

Views: 5185

Answers (2)

YoungHobbit
YoungHobbit

Reputation: 13402

 int userInputMonth = Integer.parseInt(month.toString());

It should be either of below statement:

// Read as string, convert to int.
int userInputMonth = Integer.parseInt(month.next());

// Read as int using nextInt()
int userInputMonth = month.nextInt();


Scanner day = new Scanner(System.in);

Once you create an object of Scanner, you need not to create it again. Use the same object to read the further input from the System.in. It should be either of the below statement.

// Read as string, convert to int.
int userInputDay = Integer.parseInt(month.next());

// Read as int using nextInt()    
int userInputDay = month.nextInt();

You also need to use <= condition in the day check instead of <.

if (userInputDay > 0 && userInputDay <= numberOfDaysEachMonth[userInputMonth - 1])

PS : Rename the Scanner the object like scanner or read then use it for reading both month and day.


import java.util.Scanner;

public class upp {
  public static void main(String[] args) {

    int[] numberOfDaysEachMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    System.out.println("Enter month > ");
    Scanner scanner = new Scanner(System.in);
    int userInputMonth = Integer.parseInt(scanner.next());
    if (userInputMonth > 0 && userInputMonth < 13) {
      System.out.println("Enter the day of month > ");

      int userInputDay = Integer.parseInt(scanner.next());
      if (userInputDay > 0 && userInputDay <= numberOfDaysEachMonth[userInputMonth - 1]) {
        System.out.println("Correct date.");
      } else {
        System.out.println("Wrong date.");
      }
    } else {
      System.out.println("Wrong month.");
    }
  }
}

Upvotes: 2

singhakash
singhakash

Reputation: 7919

int userInputMonth = Integer.parseInt(month.toString());

doesnt make any sense because month is a Scanner object and you converting its string representation to int which would always give you that exception instead you have to do only

int userInputMonth =  month.nextInt();

print the value of moth to check if its a valid integer or not.

You have same issue with day variable and also you can reuse month scanner instead. Full Code

try {
    int[] numberOfDaysEachMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    System.out.println("Enter month > ");
    Scanner month = new Scanner(System. in );
    int userInputMonth = month.nextInt();
    if (userInputMonth > 0 && userInputMonth < 13) {
        System.out.println("Enter the day of month > ");
        int userInputDay = month.nextInt();
        if (userInputDay > 0 && userInputDay < numberOfDaysEachMonth[userInputMonth - 1]) {
            System.out.println("Correct date.");
        } else {
            System.out.println("Wrong date.");
        }
    } else {
        System.out.println("Wrong month.");
    }
} catch (Exception e) {
    System.out.println(e);
}

DEMO

Upvotes: 1

Related Questions