Results 1 to 5 of 5
- 04-12-2011, 10:38 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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:
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.txtJava 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(); } }
- 04-12-2011, 11:09 PM #2
I have no idea what your issue is but I do wonder where you create the Worker objects and store them.
- 04-13-2011, 12:17 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
...
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
- 04-13-2011, 12:22 AM #4
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.
- 04-13-2011, 12:33 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Reading part of file with BufferedReader
By mattgavin in forum New To JavaReplies: 2Last Post: 11-13-2010, 02:44 AM -
ISSUE: Array as class constructor will not copy elements, only reference
By Lucificate in forum New To JavaReplies: 16Last Post: 07-08-2010, 09:13 PM -
Reading a file from Applet (BufferedReader)
By Java Tip in forum Java TipReplies: 1Last Post: 06-22-2008, 10:51 PM -
Reading file contents (BufferedReader)
By Java Tip in forum Java TipReplies: 0Last Post: 02-07-2008, 09:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks