0

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);
2
  • 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/… Commented Feb 1, 2012 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. Commented May 15, 2012 at 11:52

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Patient1 average[] = {p[0], p[1], p[2], p[3], p[4]}; does not work now
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.
0

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();     
} 

2 Comments

Refer this API org.apache.commons.io.FileUtils you will come to know
...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.
0

A cleaner way is:

List<SomeTypeA> listItems = retriveListOfItems(); //returns ArrayList<SomeTypeA>
SomeTypeA[] arrayOfItems = listItems.toArray(SomeTypeA[]::new);

Although, this would work as well:

List<SomeTypeA> listItems = retriveListOfItems(); //returns ArrayList<SomeTypeA>
SomeTypeA[] arrayOfItems = listItems.toArray(new SomeTypeA[0]);

Internally, it checks that the parameter passed into the toArray(SomeTypeA[]) has a length of 0, thus it copies all the items from listItems in a new array and returns it.
I prefer the first solution.

Comments

-1

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..

3 Comments

Why would you write all of that code when you can just call the api method to do it for you?
This works but now I need to take each line and put into an array like p[0] = "";
What? You're turning Patients into a String array arbitrarily? This is weird.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.