Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件中的Java输入异常_Java_Exception_File Io_Java.util.scanner - Fatal编程技术网

文件中的Java输入异常

文件中的Java输入异常,java,exception,file-io,java.util.scanner,Java,Exception,File Io,Java.util.scanner,这是我介绍的Java类的一个编程项目。我应该使用从文本文件输入的信息从终端运行程序 我可以设置它,但它总是向我抛出一个无此类元素异常。异常出现在while循环的第二轮中。我尝试将所有输入放在同一行上,并尝试在每对之间添加一条额外的行。我还尝试删除额外的输入。nextLine() 例外情况如下: Enter the price or -1 to quit: $ Is this purchase a pet? y/n: Enter the price or -1 to quit: $Exceptio

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

我可以设置它,但它总是向我抛出一个无此类元素异常。异常出现在while循环的第二轮中。我尝试将所有输入放在同一行上,并尝试在每对之间添加一条额外的行。我还尝试删除额外的输入。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
y
.98
n
1.97
n
57.89
y
12.33
n
-一,

引发异常的代码部分是:

  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
     }
  }
while((done==false)和&(nItems而((priceJavadocs for Scanner)则谈到了
nextDouble()
方法:

抛出:

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

NoTouchElementException-如果输入已耗尽

IllegalStateException-如果此扫描仪已关闭

这个错误是说这个方法没有一个double可以抓取

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

要防止这种情况,您可以执行以下操作:

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

这段代码过去了,但现在出现了一个异常,称为“未找到行”。此外,下一段询问物品是否为宠物时,将进入验证行。这段代码过去了,但现在出现了一个异常,称为“未找到行”此外,下一段询问物品是否为宠物的内容将通过验证声明,连续询问两次,我是否使用一行清除扫描仪。当我使用键盘输入从jGrasp运行程序时,程序工作正常,没有通过。此时,没有时间解决它,因此我将I’我必须按原样提交。谢谢你的帮助。