这个问题在这里已经有答案了:
为什么输入不匹配(5个答案)
上个月关门了。
import java.util.Scanner;
import java.io.*;
public class GardinierPayrollP2
{
public static void main(String[] args) throws IOException
{
// int id; //i.d. number
// double hrsworkd; //hours worked
double wkspay = 0.00; //total amount before tax for an individual employee
double netpay = 0.00; //net pay to an individual after tax
double runningTotal = 0.00; //total amount of paid salaries
double runningNetTotal = 0.00; //net total after tax
double runningTaxTotal = 0.00; //total taxes payed
double levelA = 12.00;
double levelB = 14.50;
double levelC = 16.00;
double levelD = 20.00;
//String name; //employees First Name
//char level; //level of payment for employee
final double taxRate = .08; //tax rate @ 8%
double taxes = 0.00; //dollar amount of taxes payed
File employees = new File("employees.txt");
Scanner inputFile = new Scanner(employees);
while (inputFile.hasNext())
{
String name = inputFile.nextLine();
int id = inputFile.nextInt();
char level = inputFile.next().charAt(0);
double hrsworkd = inputFile.nextDouble();
System.out.println(name + id + level + hrsworkd);
}
}
}
这就是我的工作。计算机科学二年级。还不太精通故障排除。我知道有输入错误。我就是不知道为什么。drjava的输出是打印信息的前四行,然后抛出错误。这是输出
> run GardinierPayrollP2
Rose Nylund901A10.0
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at GardinierPayrollP2.main(GardinierPayrollP2.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>
这是输入文件
Rose Nylund
901
A
10.0
Dorothy Zbornak
534
D
11.5
Blanche Deveraux
109
B
5.0
Sophia Petrillo
729
C
2.5
我做错什么了|
1条答案
按热度按时间5cnsuln71#
一定要关门
inputFile
在处理结束时。不要混合
inputFile.hasNext()
与inputFile.nextLine()
以及inputFile.nextInt()
i、 如果你想使用inputFile.nextLine()
,则应测试其相应的hasNextXXX
即inputFile.hasNextLine()
. 另外,我会用inputFile.nextLine()
并将行解析为int
或者double
因为这里讨论的问题,所以按要求。演示:
输出: