Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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 “未定义名称”;“Y”;,还有奇怪的输出问题_Java_File_Loops_Undefined - Fatal编程技术网

Java “未定义名称”;“Y”;,还有奇怪的输出问题

Java “未定义名称”;“Y”;,还有奇怪的输出问题,java,file,loops,undefined,Java,File,Loops,Undefined,我的代码可以编译,但当我尝试运行它时,当我运行Y/N循环控制变量时,它会给我一个“未定义名称”的错误。我在下面输入了整个程序的编码 此外,它在询问循环问题时显示关闭对话框。。我做错了什么 //declarations String itemName; //name of item String itemPriceInput; //user input of price double itemPrice; //parsed name of price String itemQuantityI

我的代码可以编译,但当我尝试运行它时,当我运行Y/N循环控制变量时,它会给我一个“未定义名称”的错误。我在下面输入了整个程序的编码

此外,它在询问循环问题时显示关闭对话框。。我做错了什么

//declarations
String itemName;  //name of item
String itemPriceInput;  //user input of price
double itemPrice;  //parsed name of price
String itemQuantityInput;  //user input of quantity
double itemQuantity; //parsed input of quantity
String inputMore; //loop control

//Create file instance
java.io.File itemAttributes = new java.io.File("Items_In_Stock.txt");

//Create a scanner for the file
Scanner input = new Scanner(itemAttributes);

System.out.println("Welcome to the inventory list program");  //welcome dialog for user
System.out.println("This program will show items on hand, as well as their info"); //more dialog

System.out.println("Enter 'Y' to proceed, or 'Q' to quit"); //asking to start program

inputMore=input.next(); //taking input for loop control

while (inputMore.equalsIgnoreCase("y")) //beginning outer loop

{
  while (input.hasNext()) //beginning inner loop
  {
    itemName=input.next(); // input item name
    System.out.print (" ");

    itemPriceInput=input.next(); //input item price
    itemPrice=Double.parseDouble(itemPriceInput); //parse to double
    System.out.print (" ");

    itemQuantityInput=input.next(); //input quantity
    itemQuantity=Double.parseDouble(itemQuantityInput); //parse to double

    System.out.println("Name: " + itemName + " " + "Price: " + itemPrice + " " + "Quantity: " + itemQuantity); //display input items
  } //close inner loop

  System.out.print("Enter 'Y' to input more, or 'Q' to quit"); //asking user to input more, or quit

} //close outer loop
  input.close(); //close program

  System.out.println("Thank you for using the program.  Goodbye");//goodbye statement

我看到的唯一错误是

while (inputMore.equalsIgnoreCase("y")) 
您忘记在循环结束时更新inputMore,换句话说,
inputMoreinputMore.equalsIgnoreCase(“y”)
将始终返回true,因为您从未更改它的值

你可以换成

if (inputMore.equalsIgnoreCase("y")) 
或者在循环的末尾添加另一个

inputMore = input.hext();

我假设您的
txt
文件包含如下数据

ddda 8998 787
gdfgd 8998 787
到目前为止,我可以看到两个缺陷: 当您要求用户
“输入'Y'继续,或'Q'退出”
时,您应该使用
系统中的不同扫描仪

还有,为什么要在内部使用while循环??您应该使用
条件(如果有)来请求用户选择

这是更新后的代码。它将解决您的问题:

// declarations
        String itemName; // name of item
        String itemPriceInput; // user input of price
        double itemPrice; // parsed name of price
        String itemQuantityInput; // user input of quantity
        double itemQuantity; // parsed input of quantity
        String inputMore; // loop control

        // Create file instance
        java.io.File itemAttributes = new java.io.File("Items_In_Stock.txt");

        // Create a scanner for the file
        Scanner input = new Scanner(itemAttributes);

        System.out.println("Welcome to the inventory list program");
        System.out
                .println("This program will show items on hand, as well as their info");

        System.out.println("Enter 'Y' to proceed, or 'Q' to quit");
//      inputMore = input.next(); // taking input for loop control

        Scanner sc = new Scanner(System.in);
//      System.out.println(inputMore);
        while (sc.next().equalsIgnoreCase("y")) // beginning outer loop

        {
            if(input.hasNext()) // beginning inner loop
            {
                itemName = input.next(); // input item name

                itemPriceInput = input.next(); // input item price
                itemPrice = Double.parseDouble(itemPriceInput); // parse to
                                                                // double

                itemQuantityInput = input.next(); // input quantity
                itemQuantity = Double.parseDouble(itemQuantityInput); // parse
                                                                        // to
                                                                        // double

                System.out.println("Name: " + itemName + " " + "Price: "
                        + itemPrice + " " + "Quantity: " + itemQuantity); // display
                                                                            // input
                                                                            // items
            } // close inner loop
            else
            {
                System.out.println("No more records");
                break;
            }

            System.out.print("Enter 'Y' to input more, or 'Q' to quit"); // asking
                                                                            // user
                                                                            // to
                                                                            // input
                                                                            // more,
                                                                            // or
//          inputMore = input.next();                                                               // quit

        } // close outer loop
        input.close(); // close program

        System.out.println("Thank you for using the program.  Goodbye");// goodbye
                                                                        // statement}

您能否向我们显示完整的错误消息(如果有堆栈跟踪,请显示堆栈跟踪)并指出它在哪一行?您使用的是同一个扫描仪扫描文件并请求用户输入(y/q)。如果您想从命令行获取用户输入,请使用另一个扫描器
新扫描器(System.in)
无堆栈跟踪,因为它编译得很好。完成dumb newbie quesiton。。。我通常使用gui输入,而不是扫描仪,但分配需要扫描仪。你在循环结束时更新inputMore变量是什么意思?@JeffHall对不起,我的答案不完整,我已经更新了