0

我正在用 Java 编写约会程序,遇到一个错误,即

线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“”

对于以下几行:

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at AppointmentNew.main(AppointmentNew.java:24)

该程序正在执行一次,但是一旦它第一次运行结束,它就会给我这些错误....例如,当我按如下方式运行程序时:我选择“1”进行新约会,然后我输入新约会的日期“mm/dd/yyyy”,然后添加约会描述,最后输入类型“Once, Daily, or Monthly”。完成之后,它应该从“做出选择(1:新建,2:打印范围,3:全部打印,退出):”的第一行重新开始,但它却给了我上面描述的错误......

这是我的代码。

import java.util.*;

public class AppointmentNew 
{
public static void main (String[] args)
{
  ArrayList<String> list = new ArrayList<String>();
  Scanner stdin = new Scanner(System.in);
  String choice = "";
  int choiceNum = 0;
  String date = "";
  String descrip = "";
  int type = 0;
  String typeChose = "";

  System.out.println("Welcome to Appointment App!\n");
  System.out.println("\t============================\n");

  do
  {
     System.out.print("\tMake Choice ( 1: New, 2: Print Range, 3: Print All, quit): ");
     choice = stdin.nextLine();

     choiceNum = Integer.parseInt(choice);

     if (choiceNum == 1)
     {
        System.out.print("\n\n\tEnter New Appointment Date in mm/dd/yyyy format: ");
        date = stdin.nextLine();

        System.out.print("\n\n\tEnter New Appointment Description: ");
        descrip = stdin.nextLine();

        System.out.print("\n\n\tEnter Type (1 = Once, 2 = Daily, 3 = Monthly): ");
        type = stdin.nextInt();
        if (type == 1)
        {
          Once once = new Once(date, descrip);
           typeChose = "One-Time";
        }
        else if (type == 2)
        {
          Daily daily = new Daily(date, descrip);
           typeChose = "Daily";
        }
        else
        {
          Monthly monthly = new Monthly(date, descrip);
           typeChose = "Monthly";
        }
          String stringToAdd = "";
          stringToAdd = ("\n\n\tNew " + typeChose + " Appointment Added for " + date + "\n");
          list.add(stringToAdd);

        System.out.println(stringToAdd);
        System.out.println("\t============================\n");

     }

     if (choiceNum == 2)
     {
     System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
     String lowDate = stdin.nextLine();
     System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");
     String highDate = stdin.nextLine();

     for(int i = 0; i < list.size(); i++)
        {
         int dateSpot = list.get(i).indexOf(" ");
         if (list.get(i).compareTo(lowDate) <= 0 && list.get(i).compareTo(highDate) >= 0)
        {
           System.out.println(list.get(i));   
       }}
     }

     if (choiceNum == 3)
     {
       for(int i = 0; i < list.size(); i++)
       {
          System.out.println(list.get(i));     
       }
     }

  }while (choice != "quit");      
}
}

任何帮助都会很棒!

4

2 回答 2

3

您需要在此语句之后添加对 nextLine() 的另一个调用:

type = stdin.nextInt();
// ED: stdin.nextLine();

这是因为,当您从 Scanner 获取一个 int 时,它不会消耗用户按 Enter 键时放在输入流中的 '\n' 字符。

因此,当再次调用 stdin.nextLine() 时,将返回 String "" (所有尚未处理到下一个 '\n' 字符),并且 Integer.parseInt 不知道如何处理它,所以你得到一个错误。

于 2013-04-19T02:22:29.530 回答
0

用 if 语句包围代码以在尝试解析之前检查该值是否未退出。

于 2017-05-25T12:56:15.807 回答