线程“main”java.util.NoSuchElementException中的异常

时间:2017-10-05 01:33:29

标签: java

所有编译都可以运行此程序,但它一直在给我这个  错误我试图一步一步地看到什么是错的,似乎无法找到它  有人请帮帮我,我把文本文件放在与程序相同的文件夹中  所以我不明白为什么它不期待文本文件

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at Deposit.main(Deposit.java:32)

这是主程序类:

public class Deposit
{

public static void main(String[] args) throws IOException
{

  SavingsAccount account1 = new SavingsAccount(500);
  account1.setAnnualInterest(10);
  double totalInterest = 0;


  File depositFile = new File("Deposits.txt");
  File withdrawlFile = new File("Withdrawls.txt");


  Scanner inputFile = new Scanner(depositFile);


  while(inputFile.hasNext())
  {
     account1.deposit(inputFile.nextDouble());

  }


  Scanner inputFile1 = new Scanner(withdrawlFile);


  while(inputFile1.hasNext())
  {
     account1.withdrawl(inputFile.nextDouble());

  }


 inputFile.close();
  inputFile1.close();

  totalInterest = account1.addMonthlyInterest();


  System.out.printf("Ending Balance: %f\n Total interest earned: %f" ,account1.getBalance(),totalInterest);

}
}

这是SavingAccount Class

public class SavingsAccount
{
  private double annualInterest;
  private double balance;

public void withdrawl(double userAmount)
{
  balance += userAmount;
}

public void deposit(double userAmount)
{
  balance -= userAmount;
}

public double getBalance()
{
  return balance;
}


public double addMonthlyInterest()
{
  double monthlyInterest = (( annualInterest / 12 ) * balance);

  balance += monthlyInterest;

  return monthlyInterest;
}


public void setAnnualInterest(double newRate)
{
  annualInterest = newRate / 100;
}


 public SavingsAccount(double startingBalance)
 {
  balance = startingBalance;
  annualInterest = 0;
 }

}

0 个答案:

没有答案