java - Java 输入字符串的 NumberFormatException

标签 java numberformatexception

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

Exception in thread "main" java.lang.NumberFormatException: For input string : ""

对于以下几行:

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:新建,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");      
}
}

任何帮助都会很棒!

最佳答案

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

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

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

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

关于java - Java 输入字符串的 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096171/

相关文章:

java - NDK 解析结果 : Project settings: Gradle model version=5. 1.1,NDK 版本未知

java - SnakeYaml 中的多态集合

java - 从字符串解析 Double 时出现 NumberFormatException

java - 在 Java 中读/写文件

java - Swing 组件加载时间太长?

java - 从子目录膨胀 FXML

parsing - 空安全长值

java - Integer.valueOf 阿拉伯数字工作正常,但 Float.valueOf 相同数字给出 NumberFormatException

java - 捕获异常后请求输入

java.lang.NumberFormatException : Invalid int: “” 异常