【问题标题】:Java input Exception from file来自文件的 Java 输入异常
【发布时间】:2015-06-24 04:12:18
【问题描述】:

这是我的 Java 入门课程的一个编程项目。我应该使用从文本文件输入的信息从终端运行程序。

我可以设置它,但它一直向我抛出一个无此类元素异常。异常发生在 while 循环的第二轮。我试图将所有输入放在同一行上,并尝试在每对之间添加一条额外的行。我也尝试删除额外的 input.nextLine()。

这里是个例外:

Enter the price or -1 to quit: $
Is this purchase a pet? y/n:
Enter the price or -1 to quit: $Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at DAG_PetDiscounterTester.main(DAG_PetDiscounterTester.java:33)

文件输入为:
23.56
n
178.97
n
395.88
是的
.98
n
1.97
n
57.89
是的
12.33
n
-1

引发异常的代码段是:

  while((done == false) && (nItems < ARRAY_SIZE))
  //While loop keeps the program going until user enters sentinel or array is full
  {
     Scanner input = new Scanner(System.in);   //Create a scanner

     System.out.printf("Enter the price or -1 to quit: $");
     price = input.nextDouble();
     //Inform user of required input and read it, assigning it to price

     while((price <= 0) && (price != -1))
     {
     System.out.printf("Enter the price or -1 to quit: $");
     price = input.nextDouble();
     }
     //If input is incorrect, continue to prompt until it is

     if(price == SENTINEL)   //If statement to check for sentinel
     {
        done = true;   //Changing boolean value ends the program
     }
     else  //If the program's not done, continue
     {
        input.nextLine(); //Clears newline character after price input

        System.out.printf("Is this purchase a pet? y/n: ");
        pet = input.nextLine();
        //Informs user of next information needed and assigns input to pet boolean

        while((!pet.equals("Y")) && (!pet.equals("y")) && (!pet.equals("N")) && (!pet.equals("n")))
        //Verifies user input
        {
           System.out.printf("Is this purchase a pet? y/n: ");
           pet = input.nextLine();
           //If input is incorrect, continue to prompt until it is
        }

        if((pet.equals("Y")) || (pet.equals("y")))
        //Decision statement determines what value to assign to pet boolean
        {
           isPet[nItems] = true;
        }

        else
        {
           isPet[nItems] = false;
        }

        prices[nItems] = price; //Assigns current item's price to prices array
        nItems++;   //Increments items counter to track number of items in the arrays
     }
  }

它具体发生在 price = input.nextDouble()。

任何建议将不胜感激。

【问题讨论】:

    标签: java exception file-io java.util.scanner


    【解决方案1】:

    Scanner 的 Javadocs 对 nextDouble() 方法进行了说明:

    投掷:

    InputMismatchException - 如果下一个标记与 Float 不匹配 正则表达式,或者超出范围

    NoSuchElementException - 如果输入耗尽

    IllegalStateException - 如果此扫描仪已关闭

    此错误表示该方法没有可抓取的双精度数。

    问题来自扫描器查看仅包含“n”的行并被要求从该行获取双精度,但不存在。

    为了防止这种情况,你可以这样做:

    if(input.hasNextDouble()){
        price = input.nextDouble();
    }
    

    【讨论】:

    • 这可以通过那段代码,但现在它出现了一个异常,说“找不到行”。此外,询问该物品是否为宠物的下一部分进入验证行。
    • 这行代码可以通过,但现在出现异常,提示“找不到行”。此外,下一段询问该物品是否是宠物进入验证语句,连续两次询问我是否使用线清除扫描仪。当我使用键盘输入从 jGrasp 运行程序时,它可以正常工作,而不是失败。在这一点上,没有时间解决它,所以我必须按原样提交。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多