输入字符串Java的NumberFormatException

输入字符串Java的NumberFormatException,java,numberformatexception,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(Integ

我正在用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”,然后添加约会描述,最后输入类型“一次、每日或每月”。完成后,它应该以第一行“进行选择(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");      
}
}
import java.util.*;
公共班级任命新
{
公共静态void main(字符串[]args)
{
ArrayList=新建ArrayList();
扫描仪标准输入=新扫描仪(System.in);
字符串选择=”;
int-choiceNum=0;
字符串日期=”;
字符串descripp=“”;
int类型=0;
字符串typechoosed=“”;
System.out.println(“欢迎使用预约应用!\n”);
System.out.println(“\t===========================================\n”);
做
{
System.out.print(“\t做出选择(1:新建,2:打印范围,3:全部打印,退出):”;
choice=stdin.nextLine();
choiceNum=Integer.parseInt(选项);
如果(选通==1)
{
System.out.print(“\n\n\t输入新约会日期,格式为mm/dd/yyyy:”);
date=stdin.nextLine();
System.out.print(“\n\n\t输入新约会说明:”);
descripp=stdin.nextLine();
System.out.print(“\n\n\t终端类型(1=一次,2=每日,3=每月):”;
type=stdin.nextInt();
如果(类型==1)
{
一次=新一次(日期,描述);
typechoosed=“一次”;
}
else if(类型==2)
{
每日=新的每日(日期,描述);
typechoosed=“每日”;
}
其他的
{
每月=新的每月(日期,描述);
typechoosed=“每月”;
}
字符串stringToAdd=“”;
stringToAdd=(“\n\n\t为“+date+”\n”添加的新“+TypeSelected+”约会);
列表。添加(stringToAdd);
System.out.println(stringToAdd);
System.out.println(“\t===========================================\n”);
}
如果(选择项==2)
{
System.out.print(“\n\n\t开始日期,mm/dd/yyyy格式:”);
字符串lowDate=stdin.nextLine();
System.out.print(“\n\n\t结束日期,mm/dd/yyyy格式:”);
字符串highDate=stdin.nextLine();
对于(int i=0;i

任何帮助都会很好

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

type = stdin.nextInt();
// ED: stdin.nextLine();
这是因为,当您从扫描仪获取int时,它不会使用用户点击enter时输入流中的“\n”字符


因此,当再次调用stdin.nextLine()时,将返回字符串“”(直到下一个“\n”字符之前尚未处理的所有内容),Integer.parseInt不知道如何处理该字符串,因此您会收到一个错误。

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

type = stdin.nextInt();
// ED: stdin.nextLine();
这是因为,当您从扫描仪获取int时,它不会使用用户点击enter时输入流中的“\n”字符


因此,当再次调用stdin.nextLine()时,将返回字符串“”(直到下一个“\n”字符之前尚未处理的所有内容),Integer.parseInt不知道如何处理该字符串,因此,您会得到一个错误。

在代码周围环绕一条if语句,在尝试解析之前检查值是否已退出。

在代码周围环绕一条if语句,在尝试解析之前检查值是否已退出。

当您输入一个空白时,
Integer.parseInt
无法转换为数字。因此有例外。如果(choice.equals(“”){//don parse}在您提供空白输入时,
Integer.parseInt
无法转换为数字,则使用此检查。因此有例外。如果(choice.equals(“”){//do not parse}它给我的另一个问题是当我在程序中选择第一个选项“quit”时,它给我的错误与上面描述的相同。。。。有什么建议吗?好吧,Integer.parseInt(“quit”)不太管用,是吗?你必须添加一些代码来修复这个问题:哈哈,好笑!我想了很多:/关于如何让它工作的任何建议??它给我的唯一另一个问题是,当我在程序中选择“退出”作为第一选择时,它给我的错误与上面描述的相同。。。。有什么建议吗?好吧,Integer.parseInt(“quit”)不太管用,是吗?你必须添加一些代码来修复这个问题:哈哈,好笑!我想了那么多:/有什么建议让它发挥作用吗??