This is what I have so far

try{
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("Patient.txt");
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          //Read File Line By Line
          List<String> lines = new ArrayList<String>();
          while ((strLine = br.readLine()) != null) {

          }
          //Close the input stream
          in.close();
            }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
          }
    ArrayList<Patient1> patients=new ArrayList<Patient1>();
    Patient1 p = new Patient1();
    //set value to the patient object
    patients.add(p);
    System.out.println(p);
    // File i/o

This is my txt file

001, "John", 17, 100, 65, 110, 110, 110, 109, 111, 114, 113, "Swaying, Nausea"
002, "Sun Min", 18, 101, 70, 113, 113, 110, 119, 111, 114, 113, "None"
003, "Ray Allen", 25, 103, 74, 110, 110, 110, 109, 111, 108, 109, "Difficulty driving"
004, "Michael Jordan", 26, 104, 84, 114, 115, 118, 117, 119, 120, 123, "Loss of        bladder control"
005, "Kobe Bryant", 28, 110, 80, 118, 119, 120, 119, 120, 120, 120, "None"

I have all my methods and resources all i need to do is turn the array list into an array so I could do something like

    Patient1 average[] = {p[0], p[1], p[2], p[3], p[4]};
    int totalage = 0;
    double totalweight = 0;
    for (int i=0; i<average.length; i++) {
    totalage = (average[i].getAge() + totalage);
    totalweight = (average[i].getWeight() + totalweight);
share|improve this question
Are you duplicating your own question ? [How can you pick one line from a text file and transform it into an array object?][1] [1]: stackoverflow.com/questions/9090897/… – prajeesh kumar Feb 1 at 5:27
You're doing it wrong. You should encapsulate your list in a collection and have methods that use the for-in syntax (or regular for loop in the array) to iterate and sum. You have no need to convert to an array. – Visionary Software Solutions May 15 at 11:52
feedback

3 Answers

If you wanna read a file line by line then you can use FileUtils class of common-io.

Using FileUtils.readLines() we can read file content line by line and return the result as a List of string

Example:

File file = new File("sample.txt"); 

try { 
List<String> contents = FileUtils.readLines(file); 
for (String line : contents) { 
System.out.println(line);             
} 
} catch (IOException e) { 
e.printStackTrace();     
} 
share|improve this answer
Refer this API org.apache.commons.io.FileUtils you will come to know – Nithin Kumar Reddy Feb 1 at 10:24
...Or you could just use the built in Scanner class. docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html This is available in all Java after 5. – Visionary Software Solutions May 15 at 11:49
feedback

toArray is what you want.

e.g.

ArrayList<String> l = something();
String[] foo = l.toArray(new String[foo.size()]);

ArrayList toArray

Edit

After looking at your example a little bit more. You don't need to convert it to an array, you can just call the get function on the arraylist in your loop and do your calculations. You might want to spend a little time looking at the javadoc.

share|improve this answer
Patient1 average[] = {p[0], p[1], p[2], p[3], p[4]}; does not work now – user1181810 Feb 1 at 5:24
I need the "[]" – user1181810 Feb 1 at 5:25
Your code sample says that Patient1 p = new Patient1(); Which means that p is not an array so that wouldn't have worked in the first place. – Bill Feb 1 at 5:28
feedback

Use like this

ArrayList<Patient1> patients = new ArrayList<Patient1>();
Patient1 p = new Patient1();
patients.add(p);
String[] pArray = new String[patients.size()];
pArray = patients.toArray(pArray);
for(String s : pArray)
System.out.println(s);

Hope this will help you..

share|improve this answer
Why would you write all of that code when you can just call the api method to do it for you? – Bill Feb 1 at 5:33
This works but now I need to take each line and put into an array like p[0] = ""; – user1181810 Feb 1 at 5:35
What? You're turning Patients into a String array arbitrarily? This is weird. – Visionary Software Solutions May 15 at 11:48
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.