File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes accept input about patients, write it to a file, display the file's contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "accept input about patients, write it to a file, display the file Watch "accept input about patients, write it to a file, display the file New topic
Author

accept input about patients, write it to a file, display the file's contents

Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
Hi all
I am at a bit of a hard place. Having had to fill my head with VB.net garbage, and a massive system failure, i find myself with a huge task of dealing with java's IO.
I have worked out that i need to utilise the BufferedReader in this situation, and StringTokenizer.
that's about it. i got a good mark in the programmer's exam, but having studied a whole lot of other things this year in uni, i seem to be back to square one.
any help or pointers to examples would HIGHLY and gratefully appreciated.


giddee up
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
to elaborate on what i have to do...
i need to create a medical office application, console-based.
i have to store 5 fields in a file called patients.txt
the fields are
int patientID
String name
int age
int weight
String smoker (just "true" or "false", does not have to be Boolean).

the user needs to be able to
"A" - add a new patient
"D" - delete a patient
"M" - modify the details of an existing patient
"L" - display all patient details in order of ID
"N" - display all patient details in order of name
"R" - display all patient details at risk of heart disease in order of name
"E" - Exit from the program
I have made a strong start and feel very confident in implementing the functionality of "E".... aheh heh heh ewwww. hmmmm.
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
i am working through the night over here in australia as this is all uncomfortably urgent.
Here is what i have got so far. I have the basic options, but am just going straight into inputting the details of a new patient. I am meant to keep everything in memory until the program exits - then i am meant to write it all to a text file. When the program loads up again, I am meant to read the whole contents into memory - So i guess i have to make an array of patients, to accomodate the already-existing patients, and also to add the new patients to.
???
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061

So i guess i have to make an array of patients, to accomodate the already-existing patients, and also to add the new patients to

Actually a Vector, ArrayList, or some other Container will be more convenient since it seems you need to have some resizing at run-time. This doesn't answer your most pressing question, though. I only briefly glanced at your code and it looks good so far. Can we narrow this discussion to one topic at a time? What part is causing the most trouble? Perhaps focusing on one particular method will be a good place to start.


Java API Documentation
The Java Tutorial
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
yes, you are right Layne. i would have rplied sooner, but the 5am breakfast had to occur.
i need to be able to read in a bunch of data to memory when the program first starts up. that data being, the info for each one of the pre-existing patients.
that is what i very pressing i think, to kick things off.
so i am thinking maybe ArrayList - in vb.NET, i know that ArrayLists can grow by themselves.
owww... i am SO ASHAMED. talking about vb. ugh.
anyway, each of the five fields will be on one line each, so i have to somehow read them in ... to what? creat new Patient objects from the info contained in each of the sets of five fields?
thankyou for replying Layne
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
the file that i will be reading from and writing to is called patients.txt
i suppose that the information will be contained as follows...
000001
"John Smith"
34
74
"false"
000005
"Joe Reynolds"
60
103
"true"
001467
"Mary Brown"
40
60
"false"
...
I imagine that there would not be a space between each entry. I was wondering whether each field should be comma delimited, but the specs say that the fields should appear on a line of their own.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It doesn't matter what programming language you use, most data structures are the same. Sometimes you have to implement them yourself (in straight C, for example), but in Java we can simply use the java.util package. In particular, it seems you would bew most interested in the ArrayList class. (Note: The above links are for J2SE 1.4.1. If you are using a different version, you should check The Official Java Web Site for the API docs for the version which you are using. In fact, you should thoroughly familiarize yourself with these docs. Concentrate on learning how to navigate them rather than memorizing details.)
It sounds like a Patient class can make this problem a lot more manageable as well. I can think of two approaches to initializing your Patient objects.
First a read() function with an appropriate Stream or Reader parameter could simply pull the information from a file. You would have to create a "blank" Patient object then call its read() method with an already-initialized Stream/Reader (probably in some kind of while loop in the driving function).
The second approach is to read the data from the file and call an appropriate Patient constructor with this data. Personally, I like the idea of this approach the best.
For starters, I suggest you work on reading in a single Patient. Once you figure that much out, we can move on to adding a grip of them to an ArrayList.
HTH
Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Okay, I posted that last message a bit hastily before looking over your code again. It looks like you have started a Patient class already. You should write a constructor that takes appropriate arguments to initialize the data members. You may wish to write several constructors, some of which provide default values for some of the data fields.
With that polished up, let's focus on the reading part. How are you doing with the file i/o stuff? I haven't done Java I/O for a while, so I'm a bit rusty. I'll be glad to help you out. As I noted earlier, the API docs will be of great assistance in figuring out which classes are appropriate for this particular program.
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
had a kip for a bit, up again. the fileIO stuff is being dealt with by the readIO class (i hope) ... well, it is working okay with the input from the keyboard, but it freaks if i input a string instead of an int, and i really would like to be able to have the option to re-input the field but no, it just goes on to the next field after printing a stack trace.
but that is not so important. as you say, i need to be able to read a class in from a file, and to print it to screen.
i am planning on using an ArrayList to hold the passenger objects.
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
i have been checking out the API about the StringTokenizer and stuff. Layne, when you say navigating is important, hmm, yes, very true, to actually work out how the bloody things interact and are truly implemented in a real world way!!
here are some code ideas for the patient class so far ...

what i am wondering right now is, how do i signify that ONE object's fields have been written, and then that a new object's fields are being written to file?
if there is anything that springs to mind in general about any fuzzy areas, do tell
seems to be gaining structure tho eh?
[ May 20, 2003: Message edited by: Cindy Glass ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think the save() method should have a BufferedWriter parameter (or maybe a different Writer or Stream class is more appropriate? I'm not sure). When you call save() on a patient object, then you know its data is all written to the file. It should be that simple.
As mentioned earlier, I wouldn't worry about the ArrayList yet. You can write a simple test program that creates a single patient, reads it from the file, and then writes it back to the file.

HTH
Layne
p.s. In the future will you please use proper (and consistent) indentation in your code. This will make it much easier to read.
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
thanks for the help Layne.
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
As you mention, it is very important to keep a consistant indentation (even if running on three hours sleep ;-0 ).
I have gained some insight so far on this, but now must return to a couple of other projects (a vb and oracle project) for a time. Will return to it in due course tho.
again, thankyou so much for the clarity Layne!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accept input about patients, write it to a file, display the file's contents
 
Similar Threads
(NX:URLyBird)A question about properties files:
anyone used Canvas + graphics ?
EjbTimer giving error while deployed in WebLogic 9.1
ByteOutputStream to string ?
How to handle RAISEERROR in Sybase using JDBC