输入字符串Java的NumberFormatException

时间:2013-04-19 02:18:39

标签: java numberformatexception

我正在用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”的日期,然后我添加约会描述,最后我输入“一次,每日或每月”类型。完成之后,它应该从第一行“ Make Choice(1:New,2:Print Range,3:Print All,quit)”重新开始:“但是它给了我错误我上面描述了......

这是我的代码。

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

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:3)

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

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

这是因为,当您从扫描程序中获取一个int时,它不会消耗当用户点击输入时放在输入流上的'\ n'字符。

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

答案 1 :(得分:0)

使用if语句对代码进行环绕,以在尝试解析之前检查该值是否未退出。