0

So I'm reading in information from a file and getting a type mismatch error. I ran it through the debugger but I'm not sure what's causing it - because it is reading the proper number (in this case, 2000) but it doesn't seem to think it's an integer?

My code is as follows:

public class JobSalaries {
   public static void main(String[] args) throws Exception {
       final int ARRAY_SIZE = 10;
       Employee[] employees2014, employees2015;
       int counter2014, counter2015;
       File payroll;

       payroll = new File("jobpayroll.txt");
       employees2014 = new Employee[ARRAY_SIZE];
       employees2015 = new Employee[ARRAY_SIZE];
       Scanner scan = new Scanner(payroll).useDelimiter(",| ");
       counter2014 = 0;
       counter2015 = 0;
       while(scan.hasNext()) {
         int year = scan.nextInt();
         String classification = scan.next();
         String lname = scan.next();
         String fname = scan.next();
         int monthlysalary = scan.nextInt();
         if(year == 2014 && classification.equals("Employee")) {
            String name = fname + " " + lname;
            Employee tempemployee = new Employee(name,monthlysalary);
            employees2014[counter2014] = tempemployee;
         }
       }
   }
}

And it is reading from this information:

2014 Employee Smith,John 2000

2015 Salesman Jones,Bill 3000 100000

2014 Executive Bush,George 5000 55

2014 Employee Mann,Sarah 4000

2015 Salesman Marco,Jordan 5300 440000

2014 Executive Lance,Tom 1000 60

2014 Employee Gore,Frank 7000

2015 Salesman Madison,Michelle 8000 150000

2014 Executive Berry,Ashley 7600 56

(For simplicity sake, my error is on the line: int monthlysalary = scan.nextInt();)

secondubly
  • 894
  • 5
  • 15
  • 36
  • R u sure that **"the proper number (in this case, 2000)"** does not contain any space or spacial character? – Bacteria Jun 21 '15 at 06:12
  • There is no space after it but that got me thinking - and apparently because there is a new line after it (since it is the first item in the list) it throws an error - what would I do to fix that? – secondubly Jun 21 '15 at 06:15

5 Answers5

1

The problem is that you want to jump to next line without finishing tokens on some of the lines. For instance

int monthlysalary = scan.nextInt(); results in:

2015 Salesman Jones, Bill 3000 100000
                          ^
                          monthlysalary

And then int year = scan.nextInt(); results in:

2015 Salesman Jones, Bill 3000 100000
                               ^
                               year
0

I managed to figure it out after a bit of brainstorming - the issue came from the fact that there was a new line after the first line ("technically" it is a return) and the scan was reading that in and can't parse that into an integer, so I added another delimiter for "\r" which allowed me to read it as an integer, problem solved!

secondubly
  • 894
  • 5
  • 15
  • 36
  • Are you sure? Your method expects only one integer as monthly salary where in you have multiple defined for some employees. – Ouney Jun 21 '15 at 06:30
  • I'm sure! Those numbers are going to be read - but I'm going to do so in if statements (it's wacky, I know, but it's what the assignment calls for). They're relative to "classification" which is what I'm going to use to determine whether I need to read extra numbers. – secondubly Jun 21 '15 at 06:43
0

Similar problem discussed yesterday here. I assume the root cause discussed in this thread. Please take a look at following changes:

public class JobSalaries {
    public static void main(String[] args) throws Exception {
        final int ARRAY_SIZE = 10;
        Employee[] employees2014, employees2015;
        int counter2014, counter2015;
        File payroll;

        payroll = new File("jobpayroll.txt");
        employees2014 = new Employee[ARRAY_SIZE];
        employees2015 = new Employee[ARRAY_SIZE];
        Scanner scan = new Scanner(payroll).useDelimiter(",| ");
        counter2014 = 0;
        counter2015 = 0;
        while(scan.hasNext()) {
            int year = scan.nextInt();
            String classification = scan.next();
            String lname = scan.next();
            String fname = scan.next();
          //  int monthlysalary = scan.nextInt();
            int monthlysalary = Integer.parseInt(scan.nextLine());
            if(year == 2014 && classification.equals("Employee")) {
                String name = fname + " " + lname;
                Employee tempemployee = new Employee(name,monthlysalary);
                employees2014[counter2014] = tempemployee;
            }
        }

Instead of using scan.netxtInt() try to use scan.nextLine() and parse it into an integer.

Community
  • 1
  • 1
Saleh Parsa
  • 1,362
  • 12
  • 22
0

As far as I see there are two issues with your code.

  1. Your data does not seem to be in expected format. Second line onwards you have two salaries defined which is not what program expects.
  2. You are getting InputMismatchError because you have not included newline in the useDelimiter method. Change your regex to [,\n ] and it will work.
Ouney
  • 1,108
  • 1
  • 9
  • 21
0

These are the things you could do to fix it

  1. Use delimiter [ ,\n]
  2. Since you are not reading all the token on a line, add scan.nextLine(); at the end of the loop(which will move the scanner to next line ignoring the rest tokens on the line).
barunsthakur
  • 1,106
  • 7
  • 18