异常-扫描器读取用户输入

异常-扫描器读取用户输入

我刚开始使用Java,但我以前有一些使用C#的经验。我遇到的问题是从控制台读取用户输入。

在这部分代码中,我遇到了“java.util.NoSuchElementException”错误:

payment = sc.next(); // PromptCustomerPayment function

我有两个获得用户输入的函数:

  • PromptCustomerQty
  • PromptCustomerPayment

如果我不调用PromptCustomerQty,那么我就不会得到这个错误,这使我相信我对扫描仪做了一些错误。下面是我的完整代码示例。我很感谢你的帮助。

public static void main (String[] args) {   

    // Create a customer
    // Future proofing the possabiltiies of multiple customers
    Customer customer = new Customer("Will");

    // Create object for each Product
    // (Name,Code,Description,Price)
    // Initalize Qty at 0
    Product Computer = new Product("Computer","PC1003","Basic Computer",399.99); 
    Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99);
    Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23);

    // Define internal variables 
    // ## DONT CHANGE 
    ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products
    String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output

    // Add objects to list
    ProductList.add(Computer);
    ProductList.add(Monitor);
    ProductList.add(Printer);

    // Ask users for quantities 
    PromptCustomerQty(customer, ProductList);

    // Ask user for payment method
    PromptCustomerPayment(customer);

    // Create the header
    PrintHeader(customer, formatString);

    // Create Body
    PrintBody(ProductList, formatString);   }public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) {
    // Initiate a Scanner
    Scanner scan = new Scanner(System.in);

    // **** VARIABLES ****
    int qty = 0;

    // Greet Customer
    System.out.println("Hello " + customer.getName());


慕的地8271018
浏览 469回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java