gpt4 book ai didi

java - 使用数组从文本文件创建对象(无ArrayList),运行时出现InputMismatchException

转载 作者:行者123 更新时间:2023-12-01 19:56:16 24 4
gpt4 key购买 nike

我正在开发一个项目,我必须创建从文本文件创建的对象数组。然而,每当我运行它时,我都会得到

    Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at CustomerList.main(CustomerList.java:16)

即使文本文件以整数开头。请查看我的代码并让我了解我做错了什么。

这是我必须读取的文本文件的一部分:

100

900 Amazon purchasing@amazon.com 20000.0 0.08

210 Nordstrom purchasing@nordstrom.com 50000.0 0.07

10 Rutgers purchasing@rutgers.edu 32000.0 education

520 Alamo purchasing@alamo.com 23000.0 0.05

1 Kean purchasing@kean.edu 158000.5 education

100 Allied purchasing@allied.com 85300.0 0.06

950 JoesInc purchasing@joesinc.com 999999.0 0.03

697 BostonU purchasing@tufts.edu 340020.23 education

310 TruckersInc purchasing@truckersinc.com 55000.0 0.10

820 Clothiers purchasing@clothiers.com 20044.0 0.05

849 RedCross purchasing@redcross.org 48900.2 non-profit

125 ChocolateRus purchasing@chocolate.com 3000.5 0.1

import java.util.*;
import java.math.*;
import java.text.*;
import java.io.*;

public class CustomerList {

public static void main(String[] args)throws IOException {
File f = new File("custy.txt");
Scanner in = new Scanner(f);
Customer[] obj = new Customer[1];
int count = 0;
while(in.hasNext() ){
int id = in.nextInt();
String name = in.next();
String email = in.next();
double balance = in.nextDouble();


obj[count] = new Customer (id, name, email, balance);
count++;

}

in.close();
for (int i = 0; i < count; i++)
System.out.println(obj[i]);
}



}


class Customer {

public int custId;
public String name;
public String email;
public double balance;

public Customer() {

this.custId = 0;
this.name = "";
this.email = "";
this.balance = 0.0;

}

public Customer(int custId, String name, String email, double balance) {

this.custId = custId;
this.name = name;
this.email = email;
this.balance = balance;
}

public int getId() {
return custId;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public double getBalance() {
return balance;
}

public void setName(String name) {
this.name = name;
}

public void setId(int custId) {
this.custId = custId;
}

public void setEmail(String email) {
this.email = email;
}

public void setBalance(double balance) {
this.balance = balance;
}

public String toString() {
DecimalFormat dollar = new DecimalFormat("$.00");

return custId + " " + dollar.format(balance) + name + " " + email + " ";
}


}class TaxExempt extends Customer {
public String exempt;

public TaxExempt() {
this.exempt = "";
}

public TaxExempt(int custId, String name, String email, double balance, String exempt) {
super(custId, name, email, balance);
this.exempt = exempt;

}

public String getExempt() {
return exempt;
}

public void setExempt(String reason) {
this.exempt = reason;
}

public String toString() {
DecimalFormat dollar = new DecimalFormat("$.00");
return custId + " " + name + " " + dollar.format(balance) + " " + email + " " + exempt;

}

}
class NonExempt extends Customer {
public double tax;
public double afterTax;

public NonExempt() {
this.tax = 0.0;
this.afterTax = 0.0;

}

public NonExempt(int custId, String name, String email, double balance, double tax) {
super(custId, name, email, balance);
this.tax = tax;

}

public double getTax() {
return tax;
}

public void setTax(double tax) {
this.tax = tax;
}

public void setAfterTax() {
this.afterTax = tax * balance;

}

public String toString() {
DecimalFormat dollar = new DecimalFormat("$.00");
String add = "%";
// System.out.println("Non Exempt: " );
return custId + " " + name + " " + dollar.format(balance) + " " + email + " " + tax + add + " "
+ dollar.format(afterTax);
}

}

最佳答案

您的错误与此问题有些相关:Scanner is skipping nextLine() after using next() or nextFoo()?

注意: 假设您删除了文件中的前 100

您的应用程序正在崩溃,因为当它到达时

double balance = in.nextDouble();

并尝试再迭代一次,该行中没有任何内容

int id = in.nextInt();

要读取内容,您只需包含:

in.nextLine();

之后

double++;

你应该得到你想要的输出。

完整代码示例:

import java.util.*;
import java.math.*;
import java.io.*;

class Test {
public static void main(String[] args)throws IOException {
File f = new File("custy.txt");
Scanner in = new Scanner(f);
while(in.hasNext() ){
int id = in.nextInt();
String name = in.next();
String email = in.next();
double balance = in.nextDouble();
in.nextLine();

System.out.println(id + " " + name + " " + email + " " + balance);
}
in.close();
}
}

上面的代码打印:

900 Amazon purchasing@amazon.com 20000.0
210 Nordstrom purchasing@nordstrom.com 50000.0
10 Rutgers purchasing@rutgers.edu 32000.0
520 Alamo purchasing@alamo.com 23000.0
1 Kean purchasing@kean.edu 158000.5
100 Allied purchasing@allied.com 85300.0
950 JoesInc purchasing@joesinc.com 999999.0
697 BostonU purchasing@tufts.edu 340020.23
310 TruckersInc purchasing@truckersinc.com 55000.0
820 Clothiers purchasing@clothiers.com 20044.0
849 RedCross purchasing@redcross.org 48900.2
125 ChocolateRus purchasing@chocolate.com 3000.5

关于java - 使用数组从文本文件创建对象(无ArrayList),运行时出现InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com