gpt4 book ai didi

java - NoSuchElementException异常

转载 作者:行者123 更新时间:2023-11-30 09:30:57 30 4
gpt4 key购买 nike

程序运行成功,但是一旦提示用户在最后使用封装整个程序的 while 循环再次重新运行程序,它就会抛出 NoSuchElementException,尽管在这个论坛上阅读了几个线程,但我还是不能明白为什么。任何帮助将不胜感激。

import java.util.Scanner;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;

public class billingStatement {

public static void main(String[] args) {
String again="y";
while (again.equalsIgnoreCase("y"))
{
//Declare Variables
String userName="", dateIn="";
int month=0, date=0, year=0;

// Billing Statement Header
System.out.println("Southwest Power and Light");
System.out.println("Billing Statement");

//Date, Create Template, Print Result
Date now = new Date();
SimpleDateFormat todaysDate = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("\n"+"Date: " + todaysDate.format(now));

//Initialize Scanner
Scanner scan = new Scanner(System.in);

boolean validName = false;
while (validName!= true)
{
System.out.print("Please enter your name (Last, First): ");
try
{
userName = scan.nextLine();
validName = true;
}
catch (Exception invalidName)
{
int loopCount=0;
loopCount++;
System.out.println("Unexpected input type. Please enter a valid name.");
if (loopCount==2) validName = true;
}
}

// Loop prompt until input's valid
boolean validDate = false;
while (!validDate)
{
try
{
System.out.print("Meter reading date (mm/dd/yyyy): ");
dateIn = scan.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
sdf.parse(dateIn);
validDate = true;
}
catch (Exception invalidDate)
{
System.out.println("Unexpected input. Please enter a valid date.");
}
}

// Use Delimiter
Scanner scanDate = new Scanner(dateIn);
scanDate.useDelimiter("/");
month = scanDate.nextInt();
date = scanDate.nextInt();
year = scanDate.nextInt();
scanDate.close();

//Meter Reading User Input
double powerUsed = 0;
boolean validDouble = false;
while (!validDouble)
{
try
{
Scanner scanD = new Scanner(System.in);
System.out.print("Electricity used (KW): ");
powerUsed = scanD.nextDouble();
validDouble = true;
scanD.close();
}
catch (Exception invalidDouble)
{
int loopCount=0;
loopCount++;
System.out.println("Unexpected input. Please enter a valid number.");
if (loopCount==2) validDouble = true;
}
}

//Calculate base rate via Meter Read Date
double baseRate = 0;
switch (month)
{
case 1: //January
baseRate=0.10;break;
case 2: //February
baseRate=0.10;break;
case 3: //March
baseRate=0.12;break;
case 4: //April
baseRate=0.12;break;
case 5: //May
baseRate=0.12;break;
case 6: //June
baseRate=0.15;break;
case 7: //July
baseRate=0.15;break;
case 8: //August
baseRate=0.15;break;
case 9: //September
baseRate=0.15;break;
case 10: //October
baseRate=0.15;break;
case 11: //November
baseRate=0.15;break;
case 12: //December
baseRate=0.10;break;
}

//Currency Format
NumberFormat currency = NumberFormat.getCurrencyInstance();

double totalCharge = 0;
double baseLineCharge = 0;
double baseCharge = (baseRate*powerUsed);
if(powerUsed<350)
{
baseLineCharge = powerUsed*baseRate;
}
if(powerUsed>350)
{
baseLineCharge = 350*baseRate;
}
//Calculate Total Monthly Charge for Power>350 KW
if (powerUsed<350)
{
totalCharge = baseCharge;
}
//Calculate Total Monthly Charge for 500 KW>Power>350 KW
if (powerUsed>350 & powerUsed<500)
{
totalCharge = ((baseRate*350)+((powerUsed-350)*(baseRate*1.10)));
}
//Calculate Total Monthly Charge for Power>500 KW
if (powerUsed>500)
{
double pieceChargeOne = (baseRate*350);
//System.out.println(currency.format(pieceChargeOne));
double pieceChargeTwo = ((150)*(baseRate*1.10));
//System.out.println(currency.format(pieceChargeTwo));
double pieceChargeThree = ((powerUsed-500)*(baseRate*1.25));
//System.out.println(currency.format(pieceChargeThree));
totalCharge = pieceChargeOne+pieceChargeTwo+pieceChargeThree;
}

//Print Output
System.out.println("\nName: "+ userName);
System.out.println("Meter Reading Date: " + month + "/" + date + "/" + year);
System.out.println("Electricity Used (KW): "+ powerUsed);

System.out.println("Baseline Charge: "+ currency.format(baseLineCharge));
//System.out.println("Over base Charge: "+currency.format(((powerUsed-350)*(baseRate*1.10))));
System.out.println("Total Amount Due: "+ currency.format(totalCharge));

// Prompt user for calculating another bill
Scanner scanAgain = new Scanner(System.in);
System.out.print("Calculate another bill (y/n)? ");
again = scanAgain.nextLine();

scanAgain.close();
scan.close();
}
}

这是创建 NoSuchElementException 的 block 。 scanAgain 扫描器不会读取其上方的 System.out.print 行。悲伤的一天。

        // Prompt user for calculating another bill
Scanner scanAgain = new Scanner(System.in);
System.out.print("Calculate another bill (y/n)? ");
again = scanAgain.nextLine();

scanAgain.close();
scan.close();
}
}

异常

Exception in thread "main" java.util.NoSuchElementException:
No line found at java.util.Scanner.nextLine(Unknown Source) at
billingStatement.main(billingStatement.java:173) –

最佳答案

关闭(例如 scanDate.close();)Scanner 也会关闭底层流 (System.in)。当您还没有完成从流中读取时,您不应该这样做。

关于java - NoSuchElementException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063047/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com