Results 1 to 5 of 5
  1. #1
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default BufferedReader File into Array Issue

    I haven an assignment where i have to create 5 classes

    1) Hw5
    2) Worker
    3) HourlyWorker
    4) SalariedWorker
    5) TemporaryWorker

    the task is to implement a Java program to read from payroll.txt and process the appropriate wages for the worker classes, Worker is a super class of 3,4 and 5. The Worker class is abstract. My problem is in the Hw5 class i have it set up like so to read the file and store them into an array:

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.BufferedReader;
    public class Hw5
    {
       public void readFile() 
       {
        BufferedReader br = null;
        
        try 
        {
          br = new BufferedReader(new FileReader("payroll.txt"));
          String line = null;
          
          while ((line = br.readLine()) != null) 
          {
            String[] workInfo = line.split(",");
            
            //Do necessary work with the values, here we just print them out
            for (String str : workInfo) 
            {
              System.out.println(str);
              if(str.equalsIgnoreCase("hourly"))
                System.out.println("hi"); 
              if(str.equalsIgnoreCase("Salaried"))
                System.out.println("Salary worker");
              if(str.equalsIgnoreCase("Temporary"))
                 System.out.println("Temp Worker");
            }
            System.out.println("--------------------------");
           }
        }
        catch (FileNotFoundException ex) 
        {
          ex.printStackTrace();
        }
        catch (IOException ex) 
        {
          ex.printStackTrace();
        }
        finally
        {
          try 
          {
            if (br != null)
              br.close();
          }
          catch (IOException ex)
          {
            ex.printStackTrace();
          }
        }
      }
       
    
      public static void main(String[] args) {
        Hw5 test = new Hw5();
        test.readFile();
       
    }
    }
    The issue is that it prints all of it out but when it reades the end of the line in the file, it repeats the array and the following value which is the name is stored back into the str[0] array. So if i said System.out.print(str[0]); it would print each lines str[0] array. I need it to assign the values to either temporary worker, hourly worker, or salaried worker. This is determined by the str[0] array because its either "Hourly" or "Salary". Any ideas how to fix this? Attached is the payroll.txt
    Attached Files Attached Files

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I have no idea what your issue is but I do wonder where you create the Worker objects and store them.

  3. #3
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default ...

    well, i haven't finished the code yet, right now i'm trying to test the arrays and finding out like what array[0] is array[1] is etc. the worker class has the set and get methods of name, hourlyRate, boss etc but in Hw5 i have to write it to somehow tell it to go through worker, determine if its an hourly worker or salaried worker, then run through those. The first array stored when i print the array list is hourly or salaried if the first array in [0] is hourly then the following array's 1-3 are the name, rate and boss. You can see this in the text file. But it loops and repeats the array for every line. I need it to store everything into one long array list say like 20+

    ex:
    array[0] = "hourly"; //worker type
    array[1] = "Bob";//worker name
    array[2] = 10;// worker rate
    array[3] = "wilson" //boss name

    instead of repeating back to:
    array[0] = "salaried";
    i need it to be like array[4] = "salaried";

    so i can assign them to their specified classes either hourly worker or salaried worker

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You can't do that unless you create a massive array to hold all you data which is a total waste of time. You read a line of data from the file and split it which produces the array. The next time you read a split a line of data, the array replaces the previous one. Why would you want to retain all the data? Just use the data to create your Worker objects and place them in a Collection.

  5. #5
    LifeElixer is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    hmmm you're right, i don't need an array. I don't know why i assumed i needed one. Thanks

Similar Threads

  1. Reading part of file with BufferedReader
    By mattgavin in forum New To Java
    Replies: 2
    Last Post: 11-13-2010, 02:44 AM
  2. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  3. Reading a file from Applet (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-22-2008, 10:51 PM
  4. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •