Java 程序不';不能从ZyBooks中读取文件,但可以在Eclipse上工作
ZyBooks似乎不想正常运行我的程序。我错过了什么?如果有必要,我将使用Eclipse作为IDE。Java 程序不';不能从ZyBooks中读取文件,但可以在Eclipse上工作,java,Java,ZyBooks似乎不想正常运行我的程序。我错过了什么?如果有必要,我将使用Eclipse作为IDE。 public class Main { //method1 to print Welcome Message public static void printWelcomeMessage() { System.out.println(" Program Number 1"); System.ou
public class Main {
//method1 to print Welcome Message
public static void printWelcomeMessage() {
System.out.println(" Program Number 1");
System.out.println(" Joe Blake");
System.out.println(" Computer Science 3");
}
//method2 to take input filename from user
public static String InputFileName() {
System.out.print("Enter file name:");
//Input FileName from user and return
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
//method 3 to find out Interest Rate
public static double InterestRate(double yearOfDeposit) {
//conditions
if (yearOfDeposit >= 5)
return 4.5;
else if (yearOfDeposit >= 4)
return 4;
else if (yearOfDeposit >= 3)
return 3.5;
else if (yearOfDeposit >= 2)
return 2.5;
else if (yearOfDeposit >= 1)
return 2;
else
return 1.5;
}
//method 4 to find number of times interest calculated
public static int noOfTimeInterest(char code) {
//conditions
if (code == 'A' || code == 'a')
return 4;
else if (code == 'B' || code == 'b')
return 2;
else if (code == 'C' || code == 'c')
return 1;
else if (code == 'D' || code == 'd')
return 12;
else
return 365;
}
//method 5 to calculate total earning
public static double EarnedTotal(double p, double r, int n, double t) {
double total_Interest = (p * (Math.pow((1 + (r / (100.0 * n))), (n * t))));
return total_Interest;
}
//method 6 open file
public static void openFile(String filename) {
try {
//create a file Object
File myObj = new File(filename);
//Scanner Object to read data from file
Scanner myReader = new Scanner(myObj);
//if file is not empty
if (myReader.hasNextLine()) {
System.out.println("Name Years Deposit Amount Interest Earned Total");
//loop until file have data
while (myReader.hasNextLine()) {
//read a line and store in name
String name = myReader.nextLine();
//read next line and store in data
String data = myReader.nextLine();
//split the string to fetch different values
String[] values = data.split(" ");
//fetch values
double Amount = Double.parseDouble(values[0]);
double Deposited_Years = Double.parseDouble(values[1]);
char code = values[2].charAt(0);
//calling method to find InterestRate
double rate = InterestRate(Deposited_Years);
//calling method noOfTimeInterest
int n = noOfTimeInterest(code);
//total Amount
double total = EarnedTotal(Amount, rate, n, Deposited_Years);
total = (double) Math.round(total * 100) / 100;
//calculating total_Interest by calling method InterestEarnedTotal
double total_Interest = total - Amount;
total_Interest = (double) Math.round(total_Interest * 100) / 100;
//print output and formatting
System.out.printf("%-20s %-10.0f %10s %15s %15s %n", name, Deposited_Years, "$" + Amount, "$" + total_Interest, "$" + total);
}
//print Message
System.out.println("Thank you for using interest calculation program. Have a nice day");
//close file
myReader.close();
} else { // if file is empty
System.out.println("There was no data in the file. Program terminated.");
}
} catch (FileNotFoundException e) { // if file not exists
System.out.println("Could not open file. Program terminated.");
}
}
public static void main(String[] args) {
//calling printWelcomeMessage
printWelcomeMessage();
//calling InputFileName to take Input File name
String filename = InputFileName();
//openFile for calculation
openFile(filename);
}
}
ZyBooks自动分级机给出的预期输出(assign1CS3.txt):
Program Number 1
Joe Blake
Computer Science 3
Enter file name:
Name Years Deposit Amount Interest Earned Total
Joseph Kardian III 4.00 $1500.00 $258.87 $1758.87
Joe Karlton 4.00 $3000.00 $517.74 $3517.74
Nancy Brown 6.00 $6780.00 $2075.02 $8855.0
Mike Hrady 6.00 $6780.00 $2049.32 $8829.32
Albert Wilson 12.00 $12000.00 $8350.58 $20350.58
Mohadi Mudaber 4.00 $2340.00 $405.28 $2745.28
Joe Ban Kei 2.00 $1320.00 $67.46 $1387.46
Ali Mosaker 6.00 $15500.85 $4794.46 $20295.31
Kim Ji Yoo 10.00 $7890.90 $4484.15 $12375.05
Kelly Ma Tai 4.00 $3456.90 $599.77 $4056.67
Larry Brownsen Jr. 6.00 $78650.00 $24070.83 $102720.83
Joseph Wilson 4.00 $5500.00 $954.25 $6454.25
Ali Khan 4.00 $30000.00 $5177.36 $35177.36
Nancy Jones Brown 6.00 $67800.00 $20881.81 $88681.81
Mike Alisson 2.00 $61780.00 $3147.40 $64927.40
Exception in thread "main" java.lang.NumberFormatException: empty String
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:543)
at main.openFile(main.java:85)
at main.main(main.java:129)
电流输出(以ZyBooks为单位):
Program Number 1
Joe Blake
Computer Science 3
Enter file name:
Name Years Deposit Amount Interest Earned Total
Joseph Kardian III 4.00 $1500.00 $258.87 $1758.87
Joe Karlton 4.00 $3000.00 $517.74 $3517.74
Nancy Brown 6.00 $6780.00 $2075.02 $8855.0
Mike Hrady 6.00 $6780.00 $2049.32 $8829.32
Albert Wilson 12.00 $12000.00 $8350.58 $20350.58
Mohadi Mudaber 4.00 $2340.00 $405.28 $2745.28
Joe Ban Kei 2.00 $1320.00 $67.46 $1387.46
Ali Mosaker 6.00 $15500.85 $4794.46 $20295.31
Kim Ji Yoo 10.00 $7890.90 $4484.15 $12375.05
Kelly Ma Tai 4.00 $3456.90 $599.77 $4056.67
Larry Brownsen Jr. 6.00 $78650.00 $24070.83 $102720.83
Joseph Wilson 4.00 $5500.00 $954.25 $6454.25
Ali Khan 4.00 $30000.00 $5177.36 $35177.36
Nancy Jones Brown 6.00 $67800.00 $20881.81 $88681.81
Mike Alisson 2.00 $61780.00 $3147.40 $64927.40
Exception in thread "main" java.lang.NumberFormatException: empty String
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:543)
at main.openFile(main.java:85)
at main.main(main.java:129)
这是一些填充文本,以满足文本与代码的比率要求。与
C++
相比,到目前为止,学习Java相当笨拙,尤其是在使用ZyBooks时。您的输入文件中有一个空字符串,您希望在其中读取一个双精度值(存放时间),顺便问一下,您是否有意地在println()
中使用这些空白片段?为何