JAVA字符串索引超出范围:0

时间:2019-05-27 12:23:46

标签: java while-loop

我目前正在研究日期验证程序。在显示日期结束时,程序应询问用户是否希望输入另一个日期,并且应循环播放直到用户回答“否”。我在收集用户输入后放置了一个while循环。但是我不断收到“线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0”错误。

顶部还有更多代码。它与下面的代码基本相同。

     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!");          

如果用户在打印“是否要继续”行时输入Y,则程序应循环执行,否则程序应停止。

0 个答案:

没有答案