mardi 27 septembre 2016

How to fix java.lang.numberformatexception error while using if statements

I was required to make a program that would take a user inputted date and check for several criteria: date length, leap year, month accuracy, day accuracy, etc... and I believe I have gotten everything figured out except for when I test the date "12/345/678" I get a series of Java errors including:

Exception in thread "main" java.lang.NumberFormatException: For input string: "/678" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at DateChecker.main(DateChecker.java:42)

I believe that this is because when java attempts to extract "yearString" it is unable to because it has been converted to integers using intparser and the inputted code now contains a "/" so the same error would occur with the date "1/234/5678" because monthString is looking for all integers, but it now contains a "/".

I am aware of the code below as I have read through another StackOverflow post with a similar issue, although when I tried to apply this to my program I got a series of compiler errors because my variables were now closed off from the rest of the program

try{
   int i = Integer.parseInt(input);
}catch(NumberFormatException ex){ // handle your exception
   ...
}

Here is the code that I have so far, and thanks in advance for any help that is provided :)

import java.util.*;

public class DateChecker {

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in); //activates the keyboard

    String dateString; //variable declaration

    System.out.println("Please enter a date in the format (mm/dd/yyyy): "); //prints to screen
    dateString = keyboard.nextLine();//user enters dateString value

    if (dateString.length()>10)//checks if date is greater than 10
    {
        System.out.println("Invalid date."); //prints to screen
        System.out.println("To many characters in the date.");//prints to screen
    } 
    else if(dateString.length()<10)//checks if date is less than 10
    {
        System.out.println("Invalid date.");//prints to screen
        System.out.println("To few characters in the date.");//prints to screen
    }
    else {//date = 10
        if(dateString.charAt(2)!='/' && dateString.charAt(5)!='/')//checks for "/" at spots 2 and 5
        {
            System.out.println("Invalid date.");//prints to screen
            System.out.println("Incorrect format.");//prints to screen
        }
        else{//"/" at spots 2 and 5
            //declares variables and extracts strings
            String yearString = dateString.substring(6, 10);
            String dayString = dateString.substring(3, 5);
            String monthString = dateString.substring(0, 2);

            //converts string variables to integer
            int monthInt=Integer.parseInt(monthString);
            int dayInt=Integer.parseInt(dayString);
            int yearInt=Integer.parseInt(yearString);

            if(monthInt < 1|| monthInt > 12)//checks if valid month is entered
            {
                System.out.println("Invalid date.");//prints to screen
                System.out.println("Month is not valid.");//prints to screen
            }
            else
            {//month is valid
                if(dayInt < 1)
                {
                    System.out.println("Invalid date.");
                    System.out.println("Day is not valid.");
                }

                else {
                    if((monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11) && dayInt > 30)//checks if months should have 30 days
                    {
                        System.out.println("Invalid date.");//prints to screen
                        System.out.println("Day is not valid.");//prints to screen
                    }
                    else
                        if (yearInt % 4 ==0 && (monthInt == 2 && dayInt > 29))//checks if leap year
                        {
                            System.out.println("Invalid date.");//prints to screen
                            System.out.println("Day is not valid.");//prints to screen
                        }

                        else//if not leap year
                            if (yearInt % 4 != 0 && dayInt > 28)//checks if normal year
                            {
                                System.out.println("Invalid date.");//prints to screen
                                System.out.println("Day is not valid.");//prints to screen
                            }

                            else //date entered was valid
                            {
                                System.out.println("Valid date.");
                            }
                }
            }
        }                   


    }               
}

}

Aucun commentaire:

Enregistrer un commentaire