-1

How do I read from a txt file with lines of unknown size? For example:

Family1,john,mark,ken,liam

Family2,jo,niamh,liam,adam,apple,joe

Each line has a different number of names. I am able to read in when using object type like

 family(parts[0], parts[1], parts[2]) 

but thats if I know the amout that will be in each. how do I read it in without knowing how many will be in each?

FileReader fr = new FileReader("fam.txt");
       BufferedReader br = new BufferedReader(fr);
       String fileLines;
       String[] parts;    
        while(br.ready())
        {
          fileLines = br.readLine();
          parts = fileLines.split(",");
          .
          .
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
  • What's wrong with the way you are coding it? You should end up with the parts[] array populated with the tokens from the input line. It's hard to recommend anything without knowing what the problem you're trying to solve is... – paulk23 Nov 08 '13 at 19:55

5 Answers5

2

You can use varargs for your family() method to accept the array: family(String ... parts) or just use family(String[] parts).

Personally, I would create a separate class Family and not pollute it with implementation detail about the file format (i.e. that the first item on each line is the family name):

public class Family {
    private final List<String> members = new ArrayList<>();
    private final String familyName;

    public Family(String familyName, Collection<String> members) {
        this.familyName = familyName;
        this.members.addAll(members);
    }
}

Then your loop can be like this:

List<Family> families = new ArrayList<>();

String line;
while ((line = br.readLine()) != null) {
   List<String> parts = Arrays.asList(line.split(","));
   String familyName = parts.remove(0);
   families.add(new Family(familyName, parts));
}
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

You're struggling since you try to load the data to an String[] which is in turn a plain array. You should use ArrayList that maintains an internal array and it increase its size dynamically.

FileReader fr = new FileReader("fam.txt");
BufferedReader br = new BufferedReader(fr);
String fileLines;
String[] parts;   
List<List<String>> data = new ArrayList<>(); 
while(br.ready()) {
    fileLines = br.readLine();
    parts = fileLines.split(",");
    data.add(Arrays.asList(parts));
    //do what you want/need...
}
//do what you want/need...

A better approach would be parsing the data in String[] parts and build an object of some class that will contain the data for your specific case:

public void yourMethod() {
    FileReader fr = new FileReader("fam.txt");
    BufferedReader br = new BufferedReader(fr);
    String fileLines;
    String[] parts;   
    List<Family> familyList = new ArrayList<>(); 
    while(br.ready()) {
        fileLines = br.readLine();
        parts = fileLines.split(",");
        Family family = new Family(parts[0], parts[1], parts[2]);
        familyList.add(family);
        //do what you want/need...
    }
    //do what you want/need...
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Would ArrayList do instead of List>? – Hat_To_The_Back Nov 08 '13 at 19:52
  • and what does .asList do thansk for the help? – Hat_To_The_Back Nov 08 '13 at 19:53
  • @Juan 1) [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197), and it will be better `List>` since you will store each data in `String[] parts`, it is better for maintainability than `List`. 2) [`Arrays#asList`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)). – Luiggi Mendoza Nov 08 '13 at 19:59
0

ArrayList<E>: Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null, provides methods to manipulate the size of the array that is used internally to store the list.

Each ArrayList instance has a capacity: size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

So just read your data and add to the array-list using ArrrayList.add(E e) method.

Sage
  • 15,290
  • 3
  • 33
  • 38
0

My personal preference is to use the Scanner class. You can review the functionality here. With the scanner you can parse the file a line at a time and store those values into a String.

Scanner scan = new Scanner(new File("fam.txt"));
List<String> families = new ArrayList<String>();
while(scan.hasNextLine()){
   families.add(scan.nextLine());
}

Then you can do whatever you want with the families in the ArrayList

johnsoe
  • 664
  • 1
  • 5
  • 13
0

use a Scanner, that's much easier. You can put both in a while loop

Scanner sc = new Scanner(new File(myFile.txt));
while(sc.hasNextLine()) {
  while(sc.hasNextString()) {
    String str = sc.nextString();
  }
}

This is a simplefied version and it will not work correctly, because you have to store some values in variables for Java to read them correctly. Read the documentation on the Java Scanner to find a more detailed explanation. The Scanner is much easier than what you have been doing

user2969329
  • 181
  • 1
  • 2
  • 6