简体   繁体   English

java.util.Scanner.throwFor(未知来源)。 为什么我的代码不起作用?

[英]java.util.Scanner.throwFor(Unknown Source). Why doesn't my code work?

Ok so I have written two blueprint classes and one tester.I keep getting this same error: 好的,所以我写了两个蓝图类和一个测试器。我一直遇到这个错误:

'java.util.Scanner.throwFor(Unknown Source)'. 

Can anyone enlighten me on what I am doing wrong? 谁能启发我做错了什么? Here is my code. 这是我的代码。

blueprint class ShoppingCart: 蓝图类ShoppingCart:

    package exercise3;

import java.util.Scanner;
import javax.swing.JOptionPane;
public class ShoppingCart 
{
    ScanShop amount = new ScanShop();





    public void getbill()
    {

        JOptionPane.showMessageDialog(null,"your total is: " +  amount.getcart());
    }

    public void billCal()
    {
        String answer;
        int number;
        Scanner input = new Scanner(System.in);

        /*System.out.println("please enter how much your bill is:...");
        //how much bill is:
        cart = in.nextDouble();
        in.nextLine();
        System.out.printf("you have entered: %.2f", cart);*/

        System.out.println("Do you have a loyalty card? y or n");
        // asking do you have loyalty card
        answer= input.next();


        if (answer.equalsIgnoreCase("y"))
        {

            amount.setcart(amount.getcart()*0.9);

            //other vouchers to discount
            System.out.println("thats great! do you have a voucher: "
                    + "\n(1) £5  "
                    + "\n(2) £10 "
                    + "\n (3) no vouchers");
            number= input.nextInt();
            switch(number)
            {
            case 1 :
                amount.setcart(amount.getcart()-5);
                getbill(); 
                break;

            case 2 : 
                amount.setcart(amount.getcart()-10);
                getbill();
                break;

            default : 
                getbill();
                break;
            }
        }


        else
        {
            getbill();
        }


        input.close();
    }//closing billCal

}

Blueprint ScanShop: 蓝图ScanShop:

 package exercise3;
import java.util.Scanner;
public class ScanShop 
{
private double cart;

public double getcart()
{
    return cart;
}
public void setcart(double cart)
{
    this.cart =cart;
}
public void scan()
{

    //the prices of items
    double p1;
    double p2;
    double p3;
    double total;

    Scanner in = new Scanner(System.in);

    System.out.println("what price is item one?");
    p1 = in.nextDouble();
    System.out.println("What is price of item two?");
    p2= in.nextDouble();
    System.out.println("And what is the price of the third item?");
    p3= in.nextDouble();
    in.nextLine();

      total = p1 + p2 + p3;


    System.out.printf("The total bill is %.2f\n\n", total);


    setcart(total);

    System.out.println("the cart is: " + getcart());

    in.close();




}
}

Tester class: 测试人员类别:

package exercise3;

public class ShoppingCart_Test {
public static void main (String [] arg){
    ShoppingCart customerOne = new ShoppingCart();
    //c1 is customer one
    ScanShop  c1 = new ScanShop();


    c1.scan();
    customerOne.billCal();


}
}

Problem: 问题:

Your problem is that you're closing your Scanner in the scan() method. 您的问题是您要在scan()方法中关闭Scanner Somewhat non-intuitively, closing the Scanner also closes the System.in input stream, thus causing you to be unable to create a new Scanner in the billCal() method. 以某种非直觉的方式,关闭Scanner还会关闭System.in输入流,从而使您无法在billCal()方法中创建新的Scanner

Solution: 解:

You can either A) remove in.close() at the end of your scan() method or B) create a single Scanner in the main method, and pass it to each of your methods. 您可以A)scan()方法末尾删除in.close() ,或者B)在main方法中创建单个Scanner并将其传递给每个方法。

暂无
暂无

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

相关问题 为什么不能读取此代码:线程“ main”中的异常java.util.NoSuchElementException,位于java.util.Scanner.throwFor(未知源), - Why can't read this code: Exception in thread “main” java.util.NoSuchElementException, at java.util.Scanner.throwFor(Unknown Source), 在java.util.Scanner.throwFor(未知源)错误 - at java.util.Scanner.throwFor(Unknown Source) error 线程“main”中的异常 java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) - Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) 线程“主”中的异常java.util.Scanner.throwFor(Scanner.java:862)上的java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) “线程中的异常”主“java.base/java.util.Scanner.throwFor(Scanner.Z93F725A07423FE1C889F4938B33) 处的 java.util.InputMismatchException - “Exception in thread ”main“ java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939)” 在 java.util.Scanner.nextLine(来源不明) - at java.util.Scanner.nextLine(Unknown Source) 扫描仪:java.util.Scanner.next(未知源)问题 - Scanner : java.util.Scanner.next(Unknown Source) issue 我的java.util.Scanner无法正常工作 - My java.util.Scanner won't work Java I / O程序错误throwFor(未知源) - Java i/o program error throwFor(Unknown Source) 为什么我的Java闹钟代码无法正常工作? - Why my Java alarm clock code doesn't work properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM