学校项目中带有java.util.scanner的NoTouchElementException
我正在做一个学校的项目。我的所有功能都正常工作,我在Eclipse上制作了它,它运行得非常好,但是当我通过Mimir提交项目时,在存款和取款期间,它会出现以下错误:学校项目中带有java.util.scanner的NoTouchElementException,java,exception,java.util.scanner,nosuchelementexception,Java,Exception,Java.util.scanner,Nosuchelementexception,我正在做一个学校的项目。我的所有功能都正常工作,我在Eclipse上制作了它,它运行得非常好,但是当我通过Mimir提交项目时,在存款和取款期间,它会出现以下错误: Enter the amount you want to Deposit :$Exception in thread "main" java.util.NoSuchElementException 13 at java.util.Scanner.throwFor(Scanner.java:862) 14 at
Enter the amount you want to Deposit :$Exception in thread "main" java.util.NoSuchElementException
13 at java.util.Scanner.throwFor(Scanner.java:862)
14 at java.util.Scanner.next(Scanner.java:1485)
15 at java.util.Scanner.nextDouble(Scanner.java:2345)
16 at BankAccount.deposit(BankAccount.java:24)
17 at SavingsAccountDemo.main(SavingsAccountDemo.java:30)
该项目确实说明了以下内容:请确保在尝试插入无效数据时,您的类会引发适当的异常
我试图用java.util.InputMismatchException e
和java.util.NoTouchElementException
添加try-and-catch,但它似乎无法修复它。我认为这是因为userInput是一个双精度输入,他们正在输入不同的内容。你知道我该怎么解决吗?我快发疯了,好几个小时都在努力修理它。代码如下:
///SavingsAccountDemo class
import java.util.*;
public class SavingsAccountDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double startingBalance;
double interestRate;
String userInput;
System.out.print("Enter beginning balance :$");
startingBalance = keyboard.nextDouble();
System.out.print("Enter interest rate(whole number) :%");
interestRate = keyboard.nextDouble();
double bal = startingBalance;
double rate = interestRate;
BankAccount ba = new BankAccount(startingBalance, interestRate);
BankDemo sv = new BankDemo(bal, rate);
while(startingBalance > -1) {
try {
System.out.println("Enter D for deposit" + "\nEnter W to Withdraw" + "\nEnter B for Balance" +
"\nEnter M for Monthly Process" + "\nEnter E to Exit");
userInput = keyboard.next().toLowerCase();
if("d".equals(userInput)) {
ba.deposit();
} else if("w".equals(userInput)) {
ba.withdraw();
} else if("b".equals(userInput)) {
ba.totalBalance();
} else if("m".equals(userInput)) {
ba.monthlyProcess();
} else if("e".equals(userInput)) {
ba.exit();
break;
}
else {
System.out.print("Error, option not valid\n");
}
}
catch (java.util.InputMismatchException e) {
System.out.print("Error");
}
}
}
}
///BankAccount class
import java.util.*;
public class BankAccount {
protected double balance;
protected double numDeposits;
protected double numWithdrawals;
protected double annualRate;
protected double monthlyServCharg;
protected boolean active;
Scanner keyboard = new Scanner(System.in);
public BankAccount(double startingBalance, double interestRate) {
balance = startingBalance;
annualRate = interestRate /= 100.0;
if(balance < 25) {
active = false;
} else
active = true;
}
public void deposit() {
try {
double valueD;
System.out.print("Enter the amount you want to Deposit :$");
valueD = keyboard.nextDouble();
if(valueD < 0) {
System.out.println("Error: Must enter positive value\n");
}
if( balance + valueD >= 25 && !active) {
System.out.print("Your account is now ACTIVE\n");
active = true;
}
balance += valueD;
numDeposits++;
}
catch (java.util.InputMismatchException e){
keyboard.nextLine();
}
}
public void withdraw() {
try {
double valueW;
System.out.print("Enter the amount you want to withdraw :$");
valueW = keyboard.nextDouble();
if(valueW < 0) {
System.out.println("Error: Must enter positive value\n");
}
balance -= valueW;
numWithdrawals++;
if(balance < 0) {
System.out.print("ERROR: Transaction declined!! This transaction will cause overdraft or zero balance\n");
balance += valueW;
} else if(balance <= 25 && active) {
System.out.print("Your balance is less than minimum balance. Your account is now INACTIVE\n");
active = false;
}
if(numWithdrawals > 4) {
balance --;
System.out.print("You have exceeded monthly limit of withdrawals. Fee of $1 charged\n");
}
}
catch (java.util.InputMismatchException e) {
keyboard.nextLine();
}
}
public void totalBalance() {
System.out.printf("Your Balance is: %.2f\n", balance);
}
public void calcInterest() {
double monRate = annualRate / 12;
double monInt = balance * monRate;
balance += monInt;
}
public void monthlyProcess() {
calcInterest();
balance -= monthlyServCharg;
numWithdrawals = 0;
numDeposits = 0;
monthlyServCharg = 0;
System.out.printf("Your Balance after Monthly process is: %.2f\n", balance);
}
public void exit() {
totalBalance();
System.out.print("Thank you. Bye");
}
}
///SavingsAccountDemo类
导入java.util.*;
公共类SavingsAccountDemo{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
双启动天平;
双酯;
字符串用户输入;
系统输出打印(“输入期初余额:$”;
startingBalance=keyboard.nextDouble();
系统输出打印(“输入利率(整数):%”;
interestRate=keyboard.nextDouble();
双平衡=启动平衡;
双倍利率=利率;
银行账户ba=新银行账户(起始余额、利率);
BankDemo sv=新的BankDemo(余额,费率);
同时(启动平衡>-1){
试一试{
System.out.println(“输入D表示存款”+”\n输入W表示提取”+“\n输入B表示余额”+
“\n输入M表示月度流程”+“\n输入E表示退出”);
userInput=keyboard.next().toLowerCase();
如果(“d”。等于(用户输入)){
ba.存款();
}如果(“w”。等于(用户输入)){
ba.撤回();
}else如果(“b”。等于(用户输入)){
ba.总平衡();
}如果(“m”。等于(用户输入)){
ba.monthlyProcess();
}else如果(“e”。等于(用户输入)){
ba.exit();
打破
}
否则{
System.out.print(“错误,选项无效\n”);
}
}
catch(java.util.InputMismatchException e){
系统输出打印(“错误”);
}
}
}
}
///银行帐户类别
导入java.util.*;
公营银行帐户{
保护双重平衡;
受保护的双量子点;
受保护的双纽扣;
保护双倍年率;
保护性双月休假;
受保护布尔活动;
扫描仪键盘=新扫描仪(System.in);
公共银行账户(双倍启动余额、双倍利率){
平衡=启动平衡;
年利率=利率/=100.0;
如果(余额<25){
主动=假;
}否则
主动=真;
}
公众作废保证金(){
试一试{
双重价值;
System.out.print(“输入您要存入的金额:$”;
value=keyboard.nextDouble();
if(值<0){
System.out.println(“错误:必须输入正值\n”);
}
如果(余额+值>=25&&!激活){
System.out.print(“您的帐户现在处于活动状态\n”);
主动=真;
}
余额+=估值;
numdepositis++;
}
catch(java.util.InputMismatchException e){
keyboard.nextLine();
}
}
公开撤回{
试一试{
双值;
System.out.print(“输入要提取的金额:$”;
valueW=键盘.nextDouble();
如果(值w<0){
System.out.println(“错误:必须输入正值\n”);
}
余额-=价值W;
numWithdrawals++;
如果(余额<0){
System.out.print(“错误:交易被拒绝!!此交易将导致透支或零余额”\n”);
余额+=价值W;
}否则,如果(余额4){
平衡--;
System.out.print(“您已超过每月取款限额。收取的费用为$1\n”);
}
}
catch(java.util.InputMismatchException e){
keyboard.nextLine();
}
}
公共资产负债表{
System.out.printf(“您的余额为:%.2f\n”,余额);
}
公众利益(){
双monRate=年利率/12;
双重监控=余额*利率;
余额+=监视器;
}
public void monthlyProcess(){
煅烧利息();
余额-=每月剩余费用;
numWithdrawals=0;
numDeposits=0;
monthlyServCharg=0;
System.out.printf(“您每月处理后的余额为:%.2f\n”,余额);
}
公共无效出口(){
总平衡();
系统输出打印(“谢谢你,再见”);
}
}
使用扫描仪扫描输入时,预期结果始终为字符串。
因此,您必须先检查字段是否为双精度
,然后再假定为双精度。如果强制转换引发NumberFormatException
错误,您可以使用强制转换并使用try
catch
检查。如果是这种情况,则输入无效。否则,您可以在程序中使用新铸造的double
。在执行任何操作之前,为扫描仪键盘=新扫描仪(System.in)添加验证代码>检查键盘
值是否不为空,并且仅当您检查该值是必需的时,才继续该操作value@Tom罗纳尔多你的密码在存款和取款方面都很有效,可以共享用于复制的输入error@sanjeevRm这些是他们使用的输入案例:1000 2 9 3 X 4 D 5-1 6 W 7-1 8 B 9 M 10 E我尝试了所有这些案例并在eclipse中工作,但我已经了解到,使用input的在线编辑器有时会出现问题。我要让它工作,我必须添加if(sc.hasNext())
在获取inputcalling之前,该函数将假定InputU