parcing 文本文件中的异常错误
exception Error in parcing text file
public static void main(String[] args) throws FileNotFoundException
{
printHeading();
Scanner file1 = new Scanner(new File("C:\Users\nupur\workspace\Project2\src\EmployeesIn.dat")) ;
System.out.println("New Employee added: Pryce, Lane");
System.out.println("New Wages: ");
while(file1.hasNextLine())
{
double total =0;
String line=file1.nextLine();
Scanner lineData = new Scanner(line);
String name = lineData.next().trim();
int comma = name.indexOf(',');
String last = name.substring(0, comma);
String first = lineData.next().trim();
char status = (lineData.next().trim()).charAt(0);
double wage = Double.parseDouble((lineData.nextLine().trim()));
它给我这个错误:新工资:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5.00Baird, Joey h 7.50Kinsey, Paul h 8.00Olson, Margaret s 15000Campbell, Peter s 20000Draper, Donald s 40000Sterling, Roger s 45000Cooper, Bertram s 50000"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Project2.main(Project2.java:38)
您正在尝试解析最后一行中不包含双精度数的字符串。打印您实际尝试解析的输出以追踪您的错误。
您尝试将“5.00Baird、Joey h 7.50Kinsey、Paul h 8.00Olson、Margaret s 15000Campbell、Peter s 20000Draper、Donald s 40000Sterling、Roger s 45000Cooper、Bertram s 50000”解析为双精度。
因为这不是一个数字,所以你得到了例外。
这一切都非常混乱。
你做这样的事情怎么样:
public static void main(String[] args) throws FileNotFoundException {
printHeading();
Scanner file1 = new Scanner(new File("C:\Users\nupur\workspace\Project2\src\EmployeesIn.dat")) ;
System.out.println("New Employee added: Pryce, Lane");
System.out.println("New Wages: ");
while(file1.hasNextLine()) {
System.out.println("Wage = " + file1.nextLine().replaceAll("[\D]", "")) //replace all non-digit characters with blank
}
}
public static void main(String[] args) throws FileNotFoundException
{
printHeading();
Scanner file1 = new Scanner(new File("C:\Users\nupur\workspace\Project2\src\EmployeesIn.dat")) ;
System.out.println("New Employee added: Pryce, Lane");
System.out.println("New Wages: ");
while(file1.hasNextLine())
{
double total =0;
String line=file1.nextLine();
Scanner lineData = new Scanner(line);
String name = lineData.next().trim();
int comma = name.indexOf(',');
String last = name.substring(0, comma);
String first = lineData.next().trim();
char status = (lineData.next().trim()).charAt(0);
double wage = Double.parseDouble((lineData.nextLine().trim()));
它给我这个错误:新工资:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5.00Baird, Joey h 7.50Kinsey, Paul h 8.00Olson, Margaret s 15000Campbell, Peter s 20000Draper, Donald s 40000Sterling, Roger s 45000Cooper, Bertram s 50000"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Project2.main(Project2.java:38)
您正在尝试解析最后一行中不包含双精度数的字符串。打印您实际尝试解析的输出以追踪您的错误。
您尝试将“5.00Baird、Joey h 7.50Kinsey、Paul h 8.00Olson、Margaret s 15000Campbell、Peter s 20000Draper、Donald s 40000Sterling、Roger s 45000Cooper、Bertram s 50000”解析为双精度。
因为这不是一个数字,所以你得到了例外。
这一切都非常混乱。
你做这样的事情怎么样:
public static void main(String[] args) throws FileNotFoundException {
printHeading();
Scanner file1 = new Scanner(new File("C:\Users\nupur\workspace\Project2\src\EmployeesIn.dat")) ;
System.out.println("New Employee added: Pryce, Lane");
System.out.println("New Wages: ");
while(file1.hasNextLine()) {
System.out.println("Wage = " + file1.nextLine().replaceAll("[\D]", "")) //replace all non-digit characters with blank
}
}