解析ERROR时到达文件末尾?

时间:2016-08-26 07:40:36

标签: java if-statement

我收到错误BankInterest(第93行):解析时到达文件末尾,任何人都知道我为什么会这样做?以下是我的代码,以便您确定问题所在。

提前谢谢你。

import java.nio.file.*;
import java.util.*;
import java.io;

public class BankInterest {

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

/* TASK 1: Declare variables */

String accountType;
double principal;
double rate;
double balance;
int year;

 /* Check if the expected command line is provided */

  if (args.length < 1) {
 /* Display the Usage */
   System.out.println("Usage: java BankInterest interestRateFileName");
 /* Programs quits with an error code */
   System.exit(-1);
  }
  /* TASK 2: Read interest rates from a file */
 try {

   Scanner x = new Scanner(Paths.get("commbank.txt"));
   System.out.println(x.nextDouble());
  } catch (IOException e) {

  /* TASK 3: Take user input - Which Account */

  Scanner keyboard = new Scanner(System.in);
  System.out.println("Which Account: ");
  System.out.println("1 - Savings");
  System.out.println("2 - Term Deposits");

  String line = keyboard.nextLine();

  if (line.equals("1")) {
   accountType = "Savings";
  } else if (line.equals("2")) {
   accountType = "Term Deposits";
  }

 /* TASK 4: Take user input - Principal and Period */

  Scanner input = new Scanner(System.in);
  System.out.println("Principal: ");
  principal = keyboard.nextDouble();

  System.out.println("Years: ");
  year = keyboard.nextInt();

  /* TASK 5: Calculate balance for the chosen account type */

  if (accountType == "Savings") {
   balance = principal * Math.pow((1 + rate / 100), year);
  } else if (accountType == "Term Deposits") {
   balance = (principal * rate * time) / 100;
  }

  /* TASK 6: Display results */

 if (accountType == "Savings") {
  System.out.println("");
  System.out.println("The Compound Interest is: " + balance);enter code here
 } else if (accountType == "Term Deposits") {
  System.out.println("");
  System.out.println("The Simple Interest is: " + balance);
 } else {
  System.out.println("");
  System.out.println("Error! Account is not recognized.");
 }
}

1 个答案:

答案 0 :(得分:1)

您在文件末尾错过了两个结束括号。

  • 关闭你的主要
  • 另一个关闭你的班级。

此外,在比较accountType作为评论中提到的Satya时,您应该使用equals()。