Results 1 to 2 of 2
- 10-28-2009, 05:49 PM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
A bit of a complex reading and writing problem
So here is the file I am inputting
Firstname, lastname, priority, timetofix, and TimeOfArrivalJava Code:Jon Emrich 1 30 07:00 Jill Sullivan 10 10 07:01 Frank Smith 4 45 07:25 Jacky Torres 7 160 08:00 Bob Hope 1 46 08:05 Sarah Kahn 2 35 08:10 Jessica Flint 1 20 09:00 Maggie Post 9 63 09:30 Dustin Quad 8 25 09:45 Phil Clap 7 25 10:15 Justin Hand 9 90 10:55 Blare Which 6 66 12:00 Tom Jones 4 26 12:45 Chris Johnson 7 45 13:00 Jake Wood 3 35 13:25 Chrissy Sabella 1 15 13:30 Karl Edwards 10 10 13:45 Jean Franks 5 14 14:00 Alex Smith 10 40 15:00 Frank Gore 1 35 15:30
So basically I am creating a hospital with one doctor. He checks the first person that comes, in, but if there is a queue of people in line, it will organize them by their priority, and then send them to the doctor.
Also when it's done, it writes the list of the people in order they were treated.
So basic main
Java Code:import java.util.*; import java.io.*; public class PriorityDriver { public static void main(String[] args) throws IOException { String fileName; Scanner nameReader = new Scanner(System.in); System.out.println("Enter a file name"); fileName = nameReader.nextLine(); Scanner scan = new Scanner(new FileReader(fileName)); Echo d = new Echo(fileName); d.readLines(); scan.close(); } }
Echo class,
Java Code:import java.util.*; import java.io.*; public class Echo { String fileName; // external file name Scanner scan; // Scanner object for reading from external file PriorityQueue priorityqueue = new PriorityQueue(10); int counter = 0; int counter2 = 0; int counter3 = 0; double TimeStart = 7.00; double currenttime = 7.00; double timer = 7; int number = 0; String cool = ""; Stack theStack; Object f; Object g; Object more; Object less; Object sigh; Priority ftw = new Priority(f, g, more, less, sigh); public Echo(String f) throws IOException { fileName = f; scan = new Scanner(new FileReader(fileName)); } public void readLines() { // reads lines, hands each to processLine while (scan.hasNext()) { processLine(scan.nextLine()); } scan.close(); ftw.PrintOut(); } public void processLine(String theline) { // does the real processing work StringTokenizer st = new StringTokenizer(theline); String FirstName=st.nextToken(); String LastName =st.nextToken(); String Priority= st.nextToken(); String TimeToFix=st.nextToken(); String j = st.nextToken(); if (j.charAt(0) == '0') { cool = Character.toString(j.charAt(1)) + "." + Character.toString(j.charAt(3)) + Character.toString(j.charAt(4)); } if(!(j.charAt(0)=='0')) { cool= Character.toString(j.charAt(0)) + Character.toString(j.charAt(1)) +"." + Character.toString(j.charAt(3)) + Character.toString(j.charAt(4)); } Object TimeIn=cool; ftw = new Priority(FirstName, LastName, Priority, TimeToFix, TimeIn); ftw.queing(); //problem lies here because, I am creating a new priority each time :(, so each person will then be independant of eachother, so I can't sort them properly } /* public void dotime() { String temp11 = ((String) TimeToFix.get(counter2)); String temp12 = ((String) TimeIn.get(counter2)); double m = .01 * Double.parseDouble(temp11); double g = Double.parseDouble(temp12); if(m<60) { number=1; } if(m>=.60 && m< 1.20) { number=2; } if(m>=1.20 && m < 1.80) { number =3; } if(m>=1.80 && m < 2.40) { number =4; } if(m>=2.40 && m< 3.00) { number =5; } currenttime = currenttime + m; if (currenttime > timer + .6) { currenttime = timer + number + timer - currenttime + 1; timer = timer + number; } counter2++; System.out.println(currenttime); } public void Proccessing() { }*/ }
Then the messy Priority class, just assume most of my algorithims work, even though some don't. (Dealing with time doesn't)
And the writer class I will use laterJava Code:import java.util.*; public class Priority { String FirstName; String LastName; String Priority; String TimeToFix; String TimeIn; LinkedList linkedlist = new LinkedList(); int determiner; Stack beingtreated = new Stack(); LinkedList notbeingtreated = new LinkedList(); int counter = 0; int counter2 = 0; int counter3 = 0; int counter4 = 0; int counter5 = 0; double timer = 7; int number = 0; double currenttime = 7; public Priority(Object FirstName2, Object LastName2, Object Priority2, Object TimeToFix2, Object TimeIn2) { FirstName = ((String) FirstName2); LastName = ((String) LastName2); Priority = ((String) Priority2); TimeToFix = ((String) TimeToFix2); TimeIn = ((String) TimeIn2); } public void queing() { Priority ftw = new Priority(FirstName, LastName, Priority, TimeToFix, TimeIn); if (counter3 == 0) { linkedlist.add(ftw); beingtreated.push(ftw); counter3 = 5; } else if (((notbeingtreated.size() !=0 ) && counter3!=0)) { { Priority sigh = (Priority) notbeingtreated.get(0); double win = Double.parseDouble(sigh.TimeIn); double m = (Double.parseDouble(sigh.TimeToFix)) * .01; if (m < 60) { number = 1; } if (m >= .60 && m < 1.20) { number = 2; } if (m >= 1.20 && m < 1.80) { number = 3; } if (m >= 1.80 && m < 2.40) { number = 4; } if (m >= 2.40 && m < 3.00) { number = 5; } currenttime = currenttime + m; if (currenttime > timer + .6) { currenttime = timer + number + timer - currenttime + 1; timer = timer + number; } counter2++; if (currenttime <= (Double.parseDouble(TimeIn)) * .10) { beingtreated.clear(); notbeingtreated.remove(0); } } } if ((!(beingtreated.empty())) && notbeingtreated.size() != 0) { Priority sigh = (Priority) linkedlist.get(counter - 1); double win = Double.parseDouble(sigh.TimeIn); double m = (Double.parseDouble(sigh.TimeToFix)) * .01; if (m < 60) { number = 1; } if (m >= .60 && m < 1.20) { number = 2; } if (m >= 1.20 && m < 1.80) { number = 3; } if (m >= 1.80 && m < 2.40) { number = 4; } if (m >= 2.40 && m < 3.00) { number = 5; } currenttime = currenttime + m; if (currenttime > timer + .6) { currenttime = timer + number + timer - currenttime + 1; timer = timer + number; } counter2++; if (currenttime <= (Double.parseDouble(TimeIn)) * .10) { beingtreated.clear(); } } if (beingtreated.empty()) { linkedlist.add(ftw); beingtreated.add(ftw); counter2++; } else { if (counter4 == 0) { notbeingtreated.add(ftw); counter4 = 5; } else { notbeingtreated.add(ftw); for (int i = 0; i < notbeingtreated.size() - 1; i++) { for (int j = i + 1; j < notbeingtreated.size(); j++) { Priority mmm = (Priority) notbeingtreated.get(i); Priority next = (Priority) notbeingtreated.get(i - 1); if (Double.parseDouble(mmm.GetPriority()) > Double.parseDouble(next.GetPriority())) { double temp = Double.parseDouble(mmm.GetPriority()); notbeingtreated.set(i, next); notbeingtreated.set(j, mmm); } } } } } counter++; } public void compute() { } public String GetFirstName() { return FirstName; } public String GetLastName() { return LastName; } public String GetPriority() { return Priority; } public String GetTimeToFix() { return TimeToFix; } public String GetTimeIn() { return TimeIn; } public void PrintOut() { for (int vrz = 0; vrz < linkedlist.size(); vrz++) { Priority wasd = (Priority) linkedlist.get(vrz); System.out.print(wasd.GetFirstName() + " "); System.out.print(wasd.GetLastName() + " "); System.out.print(wasd.GetPriority()+ " "); System.out.print(wasd.GetTimeToFix()+" "); System.out.print(wasd.GetTimeIn()+" "); System.out.println(" "); } } }
Java Code:import java.io.*; import java.util.*; public class Writer{ String fileName; public Writer(Priority ftw) throws IOException { PrintWriter writer = new PrintWriter("List.txt"); writer.write(ftw.FirstName); writer.write(ftw.LastName); writer.write(ftw.Priority); writer.write(ftw.TimeIn); writer.write(ftw.TimeToFix); writer.close(); } }
So I am creating a new Priority eachtime in Echo, so that is a problem because the variables won't be the same from the previous time. I think I am missing something very fundamental. I don't really want you guys to read the whole code, it's just too much, and it's messy.
- 10-28-2009, 06:08 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Yep the structure is not right.
Your Priority class should only contain the fields being read, appropriate constructors, an equals method, a hashCode method, a compareTo method and a toString.
Then you need a class that contains a TreeSet and a method that uses a loop to read the file line by line creating a Priority inside the loop and adding that Priority to the TreeSet.
Similar Threads
-
Reading and writing to a file
By jigglywiggly in forum New To JavaReplies: 13Last Post: 03-09-2009, 10:44 AM -
Reading/Writing to file
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-28-2008, 02:05 PM -
writing/reading a Structure to/from a file
By Farzaneh in forum New To JavaReplies: 1Last Post: 08-31-2008, 02:21 PM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks