java.util.NoSuchElementException-扫描仪读取用户输入
我对使用Java还不熟悉,但我以前有一些使用C#的经验。我遇到的问题是从控制台读取用户输入 我在这部分代码中遇到了“java.util.NoSuchElementException”错误:java.util.NoSuchElementException-扫描仪读取用户输入,java,input,java.util.scanner,Java,Input,Java.util.scanner,我对使用Java还不熟悉,但我以前有一些使用C#的经验。我遇到的问题是从控制台读取用户输入 我在这部分代码中遇到了“java.util.NoSuchElementException”错误: payment = sc.next(); // PromptCustomerPayment function 我有两个获取用户输入的函数: 提示客户数量 即期客户付款 若我不打电话给PromptCustomerQty,那个么我就不会收到这个错误,这让我相信我的扫描仪出了问题。下面是我的完整代码示例。谢谢
payment = sc.next(); // PromptCustomerPayment function
我有两个获取用户输入的函数:
- 提示客户数量
- 即期客户付款
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());
// Loop through each item and ask for qty desired
for (Product p : ProductList) {
do {
// Ask user for qty
System.out.println("How many would you like for product: " + p.name);
System.out.print("> ");
// Get input and set qty for the object
qty = scan.nextInt();
}
while (qty < 0); // Validation
p.setQty(qty); // Set qty for object
qty = 0; // Reset count
}
// Cleanup
scan.close();
}
public static void PromptCustomerPayment (Customer customer) {
// Initiate Scanner
Scanner sc = new Scanner(System.in);
// Variables
String payment = "";
// Prompt User
do {
System.out.println("Would you like to pay in full? [Yes/No]");
System.out.print("> ");
payment = sc.next();
} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));
// Check/set result
if (payment.toLowerCase() == "yes") {
customer.setPaidInFull(true);
}
else {
customer.setPaidInFull(false);
}
// Cleanup
sc.close();
}
publicstaticvoidmain(字符串[]args){
//创建客户
//未来证明多个客户的可能性
客户=新客户(“遗嘱”);
//为每个产品创建对象
//(名称、代码、说明、价格)
//初始数量为0
产品计算机=新产品(“计算机”、“PC1003”、“基本计算机”,399.99);
产品监视器=新产品(“监视器”、“MN1003”、“LCD监视器”,99.99);
产品打印机=新产品(“打印机”、“PR1003x”、“喷墨打印机”,54.23);
//定义内部变量
//##不要改变
ArrayList ProductList=新建ArrayList();//用于存储产品的列表
String formatString=“%-15s%-10s%-20s%-10s%-10s%n”;//输出的默认格式
//将对象添加到列表中
ProductList.add(计算机);
ProductList.add(监视器);
ProductList.add(打印机);
//询问用户数量
PromptCustomerQty(客户、产品列表);
//向用户询问付款方式
提示客户付款(客户);
//创建标题
打印头(客户,格式字符串);
//创造身体
打印体(ProductList,formatString);
}
公共静态无效提示客户数量(客户、ArrayList ProductList){
//启动扫描仪
扫描仪扫描=新扫描仪(System.in);
//****变量****
整数数量=0;
//问候顾客
System.out.println(“Hello”+customer.getName());
//循环浏览每个项目并询问所需数量
对于(产品p:ProductList){
做{
//询问用户数量
System.out.println(“您想要多少产品:+p.name”);
系统输出打印(“>”);
//获取输入并设置对象的数量
数量=扫描.nextInt();
}
while(数量<0);//验证
p、 设置数量(数量);//设置对象的数量
数量=0;//重置计数
}
//清理
scan.close();
}
公共静态无效提示客户付款(客户){
//启动扫描仪
扫描仪sc=新的扫描仪(System.in);
//变数
字符串付款=”;
//提示用户
做{
System.out.println(“您想全额付款吗?[是/否]”);
系统输出打印(“>”);
付款=sc.next();
}而((!payment.toLowerCase().equals(“yes”)&(!payment.toLowerCase().equals(“no”);
//检查/设置结果
如果(payment.toLowerCase()=“是”){
customer.setPaidInFull(true);
}
否则{
customer.setPaidInFull(false);
}
//清理
sc.close();
}
问题是
当扫描仪关闭时,如果输入源实现可关闭接口,它将关闭输入源
因此scan.close()
关闭System.in
要解决这个问题,你可以
扫描仪扫描
静态
并且不要在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);
}
static Scanner scan;
public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) {
// Initiate a Scanner
scan = new Scanner(System.in);
// **** VARIABLES ****
int qty = 0;
// Greet Customer
System.out.println("Hello " + customer.getName());
// Loop through each item and ask for qty desired
for (Product p : ProductList) {
do {
// Ask user for qty
System.out.println("How many would you like for product: " + p.name);
System.out.print("> ");
// Get input and set qty for the object
qty = scan.nextInt();
}
while (qty < 0); // Validation
p.setQty(qty); // Set qty for object
qty = 0; // Reset count
}
// Cleanup
}
public static void PromptCustomerPayment (Customer customer) {
// Variables
String payment = "";
// Prompt User
do {
System.out.println("Would you like to pay in full? [Yes/No]");
System.out.print("> ");
payment = scan.next();
} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));
// Check/set result
if (payment.toLowerCase() == "yes") {
customer.setPaidInFull(true);
}
else {
customer.setPaidInFull(false);
}
}
publicstaticvoidmain(字符串[]args){
//创建客户
//未来证明多个客户的可能性
客户=新客户(“遗嘱”);
//为每个产品创建对象
//(名称、代码、说明、价格)
//初始数量为0
产品计算机=新产品(“计算机”、“PC1003”、“基本计算机”,399.99);
产品监视器=新产品(“监视器”、“MN1003”、“LCD监视器”,99.99);
产品打印机=新产品(“打印机”、“PR1003x”、“喷墨打印机”,54.23);
//定义内部变量
//##不要改变
ArrayList ProductList=新建ArrayList();//用于存储产品的列表
String formatString=“%-15s%-10s%-20s%-10s%-10s%n”;//输出的默认格式
//将对象添加到列表中
ProductList.add(计算机);
ProductList.add(监视器);
ProductList.add(打印机);
//询问用户数量
PromptCustomerQty(客户、产品列表);
//向用户询问付款方式
提示客户付款(客户);
//创建标题
打印头(客户,格式字符串);
//创造身体
打印体(ProductList,formatString);
}
静态扫描;
公共静态无效提示客户数量(客户、ArrayList ProductList){
//启动扫描仪
扫描=新扫描仪(System.in);
//****变量****
整数数量=0;
//问候顾客
System.out.println(“Hello”+customer.getName());
//循环浏览每个项目并询问所需数量
对于(产品p:ProductList){
做{
//询问用户数量
System.out.println(“您想要多少产品:+p.name”);
系统输出打印(“>”);
//获取输入并设置对象的数量
数量=扫描.nextInt();
}
while(数量<0);//验证
p、 设置数量(数量);//设置对象的数量
数量=0;//重置计数
}
//清理
}
公共静态无效提示客户付款(客户){
//变数
字符串付款=”;
//提示用户
做{
System.out.println(“您想全额付款吗?[是/否]”);
系统输出打印(“>”);
payment=scan.next();
}而((!payment.toLowerCase().equals(“yes”)&(!payment.toLowerCase().equals(“no”);
//检查/设置结果
如果(payment.toLowerCase()=“是”){
customer.setPaidInFull(true);
}
否则{
customer.setPaidInFull(false);
}
}
另一方面,您不应该使用
==
进行字符串比较,而应该使用.equals
。这确实让我困惑了一段时间,但这就是我最终发现的。
在第一种方法中,当您调用,sc.close()
时,它不仅会关闭您的扫描仪,还会关闭您的计算机
System.out.println(System.in.available());
Scanner scanner = new Scanner(System.in);
// Ask users for quantities
PromptCustomerQty(customer, ProductList, scanner );
// Ask user for payment method
PromptCustomerPayment(customer, scanner );
//close the scanner
scanner.close();
public static void PromptCustomerQty(Customer customer,
ArrayList<Product> ProductList, Scanner scanner) {
// no more scanner instantiation
...
// no more scanner close
}
public static void PromptCustomerPayment (Customer customer, Scanner sc) {
// no more scanner instantiation
...
// no more scanner close
}
public class Reader {
private Scanner reader;
private static Reader singleton = null;
private boolean alreadyClosed;
private Reader() {
alreadyClosed = false;
reader = new Scanner(System.in);
}
public static Reader getInstance() {
if(singleton == null) {
singleton = new Reader();
}
return singleton;
}
public int nextInt() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextInt();
}
throw new AlreadyClosedException(); //Custom exception
}
public double nextDouble() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextDouble();
}
throw new AlreadyClosedException();
}
public String nextLine() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextLine();
}
throw new AlreadyClosedException();
}
public void close() {
alreadyClosed = true;
reader.close();
}
}