I'm working on a project where I have to make an array of objects created from a text file. However whenever I run it, I get
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)
Even though the text file starts with an integer. Please look at my code and give me some insight into what I'm doing wrong.
This is part of the text file that I have to read from:
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);
}
}