Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with reading from a file in java
    primarykey
    data
    text
    <p>I'm trying to read through a file and create some objects/print information in a different format.</p> <p>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).</p> <p>I've done some research on the issue, and I <em>think</em> 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?</p> <p>Thank you for any help. I have asked several questions here before and you never fail to help me learn!</p> <p>Code</p> <pre><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(); } } </code></pre> <p>File I'm trying to read from</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload