0

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);
}

}
Readyeddy
  • 5
  • 5

1 Answers1

0

Your error is somewhat related to this question: Scanner is skipping nextLine() after using next() or nextFoo()?

Note: Assuming you removed the first 100 in the file

Your app is crashing because when it gets to

double balance = in.nextDouble();

And tries to iterate one more time, there's nothing in that line for

int id = in.nextInt();

To read from, all you need is to include:

in.nextLine();

After

double++;

And you should get your desired output.

Complete code as example:

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();
    }    
}

The above code prints:

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
Frakcool
  • 10,088
  • 9
  • 41
  • 71
  • THANK YOU! It works now. I just have to figure out how to edit the output. Thank you again. Is there a way to set the size of the array to the first number instead of typing 100 in the brackets? – Readyeddy Nov 25 '19 at 22:03
  • You read it before `while(in.hasNext())` and create your `Customer` array with that number instead of `1` in the square braces. If this answer solved your question, be sure to accept it – Frakcool Nov 25 '19 at 22:06