0

Im currently working on a date validation program. At the end of displaying a date, the program should ask the user if they wish to enter another date and should loop around until the user answers no. I place a while loop after gathering input from the user. However i keep on getting "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0" error.

There are more code at the top. It basically the same codes as below.

     System.out.println("Do you wish to continue?: ");
     String input=reader.nextLine();
     char option=input.toLowerCase().charAt(0);

     while(option=='y')
       {
             System.out.println("Enter the day for today's date: ");
             day=reader.nextInt();

             System.out.println("Enter the month for today's date: ");
             month=reader.nextInt();

             System.out.println("Enter the year for today's date: ");
             year=reader.nextInt();  

             if(year<1800 || year>3000)
                System.out.println("***ERROR: Invalid input - this is not 
                                   a legal date.");

            else if(month==4 || month==6 || month==9 || month==11)
                {even(day,month,year);}

            else if (month==1 || month==3 || month==5 || month==7 || 
                     month==8 || month==10)
                {odd(day,month,year);}

            else if(month==2)
                {February(day,month,year);}

            else if(month==12)
                {December(day,month,year);}

            else
                {System.out.println("***ERROR: Invalid input - this is not 
                                     a legal date.");}  

            System.out.println("Do you wish to continue?: ");
            input=reader.nextLine();
            option=input.toLowerCase().charAt(0);  
        }

     System.out.println("End of program!");          

If user enters Y when the line "Do you wish to continue is printed", the program should loop else the program should stop.

Md Fairus
  • 17
  • 5

0 Answers0