-5

I am building a sorting program for a class, and this whole week I've been stuck on how to read in the text file. The text file will be specified as a command argument on command line, and it will consist of hospital records. it will be 4 pieces of data separated by comma on each line. it will be someones last, and first name, room number, and age. I have to read in this data somehow line by line. number for peoples records aren't specified. I know how to sort them, I just havent been able to figure out how to read in the data. this is an example of what it looks like.

Costanza,George,122,53 Poppins,Mary,123,72

Dayman
  • 67
  • 1
  • 7
  • "I've been stuck on how to" "I just haven't been able to" Why? Be specific on what you're having trouble with. – Shashank Mar 20 '15 at 01:37
  • You should try to post whatever code you've tried writing so we don't have to start from scratch. Also, you may want to look into the classes in java.io package (BufferedReader, FileReader, etc.) – Edward L. Mar 20 '15 at 01:37
  • 3
    Possible duplicate of [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file) A bit of a stretch but the problem is almost identical. – Nic Mar 20 '15 at 01:38

1 Answers1

0

You could read in the file line by line, splitting the line at the commas, storing the split string in an array, and setting fields accordingly. Do you have a class for the patient?

Here is an example that uses similar methods that may be applicable to your situation.

while(in.hasNextLine()){        
    line = in.nextLine();
    studentTraits = line.split(" \\| ");
    ...}
//studentTraits is an array with 5 indexes, and
//each line of the file has 5 sections separated by the pipe character

Hope this helps. Next time you ask a question, it will be much more helpful if you asked a much more specific question. Here, you did not exactly ask a question, you just pretty much asked for someone to write some code for you, and it doesn't look like you put any effort into solving the problem for yourself. Please show what you know, and ask about what you are stuck on.

peter
  • 78
  • 9
  • Sorry,this is my first question on here ever. Ive spent a week on it and ive started over 5 times. I dont think my code would be much help. Buffer reader wont help because I need the user to be able to input the code from the command line. I just need an idea to work from. you just gave me an idea though, this will go a long way. thank you for the advice for next time. – Dayman Mar 20 '15 at 02:15