简体   繁体   English

输入字符串Java的NumberFormatException

[英]NumberFormatException for Input String Java

I am writing an appointment program in Java and am coming across an error which is 我正在用Java写一个约会程序,遇到一个错误

Exception in thread "main" java.lang.NumberFormatException: For input string : "" 线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:“”

for the following lines : 对于以下几行:

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)

The program is going through once, but once it gets to the end of its first run it gives me those errors.... For instance when I run the program as follows : I make the choice of "1" to make a new appointment, I then enter the date of my new appointment "mm/dd/yyyy", then I add an appointment description, and lastly I enter the type "Once, Daily, or Monthly". 该程序执行一次,但是一旦到达第一次运行的末尾,它就会给我这些错误...。例如,当我按如下方式运行程序时:我选择“ 1”进行新的约会,然后输入新约会的日期“ mm / dd / yyyy”,然后添加约会描述,最后输入“一次,每天或每月”类型。 After that finishes it should start back over with the very first line of " Make Choice (1: New, 2: Print Range, 3: Print All, quit):" But instead it gives me the errors I described above... 完成之后,它应该从“ 做出选择(1:新建,2:打印范围,3:全部打印,退出):”的第一行重新开始但是相反,它给了我上面描述的错误...

Here is my code I have. 这是我的代码。

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

Any help would be great! 任何帮助将是巨大的!

You need to add another call to nextLine() after this statement here: 您需要在此语句之后在此处添加另一个对nextLine()的调用:

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

This is because, when you grab an int from the Scanner, it doesn't consume the '\\n' character that gets put on the input stream when the user hits enter. 这是因为,当您从Scanner中获取一个int时,它不会占用当用户按下Enter时输入流上放置的'\\ n'字符。

Thus, when stdin.nextLine() is called again, the String "" is returned (everything not yet processed up to the next '\\n' character), and Integer.parseInt doesn't know how to handle that, so you get an error. 因此,当再次调用stdin.nextLine()时,将返回字符串“”(直到下一个'\\ n'字符为止尚未处理的所有内容),并且Integer.parseInt不知道如何处理,因此您得到了一个错误。

用if语句将代码括起来,以在尝试解析该值之前检查该值是否未退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM