0

I'm trying to read through a file and create some objects/print information in a different format.

I've got most of it correct, but for some reason when it loops through I get "String index out of range: 0" every time it finishes one if statement (It will print the first object before it happens).

I've done some research on the issue, and I think it's happening because of the next line looking for a char at position 0 again (ID.charAt(0)). However, I have another program I've done this way and it works perfectly. Is there any chance y'all could take a look at it and show me where I might have messed up?

Thank you for any help. I have asked several questions here before and you never fail to help me learn!

Code

import java.io.File;
import java.util.Scanner;
public class PayrollSystemTest2 {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Scanner input;
        input =  new Scanner(new File("EmployeePayrollInfo.txt"));
        Employee[] Emp = new Employee[20];

        while(input.hasNext())
        {
            String ID = input.nextLine();

            if (ID.charAt(0) == 'S')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                double salary = input.nextDouble();
                Emp[0] = new SalariedEmployee(first, last, ssn, DayOfBirth, ID, salary);
                System.out.println(Emp[0].toString());
            }
            else if (ID.charAt(0) == 'H')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                double hourlyWage = input.nextDouble();
                double hoursWorked = input.nextDouble();
                Emp[1] = new HourlyEmployee(first,last,ssn,DayOfBirth,ID,hourlyWage,hoursWorked);
                System.out.println(Emp[1].toString());
            }
            else if (ID.charAt(0) == 'C')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                Double sales = input.nextDouble();
                Double rate = input.nextDouble();
                Emp[2] = new CommissionEmployee(first,last,ssn,DayOfBirth,ID,sales,rate);
                System.out.println(Emp[2].toString());
            }
            else if (ID.charAt(0) == 'B')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                Double sales = input.nextDouble();
                Double rate = input.nextDouble();
                Double salary = input.nextDouble();
                Emp[3] = new BasePlusCommissionEmployee(first,last,ssn,DayOfBirth,ID,sales,rate,salary);
                System.out.println(Emp[3].toString());
            }
            else if (ID.charAt(0) == 'P')
            {
                String first = input.nextLine();
                String last = input.nextLine();
                String ssn = input.nextLine();
                Date DayOfBirth = new Date(input.nextInt(),input.nextInt(),input.nextInt());
                Double Wage = input.nextDouble();
                Double Pieces = input.nextDouble();
                Emp[4] = new PieceWorker(first,last,ssn,DayOfBirth,ID,Wage,Pieces);
                System.out.println(Emp[4].toString());
            }

        }
        input.close();


    }
}

File I'm trying to read from

S100
Sully
Ross
111-11-1111
8 15 1979
900.00
H205
Joe
Aggie
222-22-2222
3 6 1993
15.25
40
C102
Rev
Elee
333-33-3333
11 22 1985
20000
.065
B115
Johnny
Football
444-44-4444
06 28 1965
12000
.05
400
P206
Miss
Bizbee
555-55-5555
11 06 1977
1.25
1000
X
user2774647
  • 75
  • 4
  • 12

3 Answers3

3

This applies to all you blocks, but basically when you do...

double salary = input.nextDouble();

It is leaving the new line character in the scanner, so when you do the next String ID = input.nextLine();, it is reading the remainder of the line, which is basically blank...

Try adding input.nextLine(); at the end of each section, for example...

double salary = input.nextDouble();
input.nextLine();

Also, your DayOfBirth is wrong. Apart from being deprecated, the Date constructor expects the values in year, month, day order ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you so much! That makes a lot more sense now and everything works perfectly. I was unaware that nextDouble() stayed on the same line. Thanks again! – user2774647 Nov 20 '13 at 04:41
  • @user2774647 You date's of births are wrong, watch out for those. The input seems to be month, day, year – MadProgrammer Nov 20 '13 at 04:42
  • Yeah, I actually created my own Date class for this which is in Month, Day, Year order so it actually outputs correctly on my end. Sorry for not mentioning that! – user2774647 Nov 20 '13 at 04:52
  • 1
    So long as it doesn't suddenly freak you out ;) – MadProgrammer Nov 20 '13 at 04:53
0

It is possible that the first line of your input has no content, other than just being a blank line (a new line character). Try to print out the ID variable BEFORE the first if statement, and see what you end up with.

Sherz
  • 568
  • 2
  • 14
0

My reason is similar to MadProgrammer reason but instead of adding input.nextLine(); at the end of each section we can add

if(input.hasNext()){

input.nextLine(); 

}

at the end of while loop only once.