首页 > 解决方案 > 使用数组从文本文件创建对象,(无 ArrayList),运行时 InputMismatchException

问题描述

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

    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.com 20000.0 0.08

210 诺德斯特龙采购@nordstrom.com 50000.0 0.07

10 罗格斯大学采购@rutgers.edu 32000.0 教育

520 阿拉莫采购@alamo.com 23000.0 0.05

1 Kean purchase@kean.edu 158000.5 教育

100 联合采购@allied.com 85300.0 0.06

950 JoesInc 采购@joesinc.com 999999.0 0.03

697 BostonU purchase@tufts.edu 340020.23 教育

310 TruckersInc purchase@truckersinc.com 55000.0 0.10

820 Clothiers purchase@clothiers.com 20044.0 0.05

849 RedCross purchase@redcross.org 48900.2 非营利组织

125 ChocolateRus purchase@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);
}

}

标签: javaarraysoopobject

解决方案


您的错误与以下问题有些相关: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

推荐阅读