Pregunta

I have written code for reading a structured file in Java, and extracting the line contents to create objects (of type Person).

The file has the following structure: [personNumber], [personName], [personAge], for example

0, Sherlock Holmes, 44

1, Harry Potter, 17

2, Jack Sparrow, 50

...

The code for reading the file and extracting the information is:

        RandomAccessFile rf = new RandomAccessFile(fileName, "rw");
        String line;
        File myFile = new File(fileName);
        scan = new Scanner(myFile);
        String[] split;

        //Read file
        while((line = rf.readLine()) != null) {
                    split = scan.nextLine().split(", ");
                    pnumber = Integer.parseInt(split[0]);
                    name = split[1];
                    age = Integer.parseInt(split[2]);
                    thePerson = new Person(name, age, pnumber);
                    personList.addLast(thePerson);
        }

This works well, and the Person-objects are correctly added to a singly linked list that I have programmed. No problems here.

However, the program is also supposed to be able to read a text file of the following format:

#People

0, Sherlock Holmes, 44

1, Harry Potter, 17

2, Jack Sparrow, 50

#Dogs

0, Scooby, 10

1, Milou, 7

2, Scar, 15

Is there a way to check if the line being read contains a hash (#) symbol, in which case the program understands that it is not to split this line, but simply that a new category is starting? So that you can decide which type of object you want to create from following lines, for instance person og dog objects. For simplicity, the order of the types (Person, Dog) will always be the same, but the number of lines following each category will vary. Hence, I need help with figuring out if a line contains a #-symbol, indicating the start of a new category, and only create objects from the following lines.

¿Fue útil?

Solución

or if your # is always at the beginning of the string:

EDIT:

while((line = rf.readLine()) != null) {
    if(line.charAt(0)=='#'){
        scan.nextLine(); //use it to suit your need..
        //alert the program to prepare for new type of object
        //do something..
    }else {
        split = scan.nextLine().split(", ");
                pnumber = Integer.parseInt(split[0]);
                name = split[1];
                age = Integer.parseInt(split[2]);
                thePerson = new Person(name, age, pnumber);
                personList.addLast(thePerson);
    }
}

Otros consejos

As this looks like homework, I will only answer your first question and let you figure out how to use it in your program. The way to check if the line you read contains a hash symbol is this:

line = scan.nextLine();
if(line.contains('#')){
   // the line had a #
}
else{
   //it did not.
}

You can do this:

String line = null;
String currentType = null;
while((line = rf.readLine()) != null) {
    if (line.startsWith("#")) {
        currentType = line.substring(1);
        continue;
    }
    split = line.split(",");
    if (currentType.equals("People")) {
        pnumber = Integer.parseInt(split[0]);
        name = split[1];
        age = Integer.parseInt(split[2]);
        thePerson = new Person(name, age, pnumber);
        personList.addLast(thePerson);
    } else if (currentType.equals("Dogs")) {
        ...
        Dog dog = new Dog(name,age);
    }           
}

First assign readed line to a variable:

String line = scan.nextLine();

Then apply logic to its value:

if (line.startsWith("#")) {
    // check which type of object is declared
} else {
    // parse line according to object type
    // add object to list
}

Also I recommend using separate parser for separate type of objects. I.e. DogLineParser, PersonLineParser, etc. all of which would implement common interface. Example interface could look like this

public interface LineParser<T> {
    T parseLine(String line);
}

And example implementation could look like this:

public class PersonLineParser extends LineParser<Person> {
    @Override
    public Person parseLine(String line) {
        String[] split = line.split(", ");
        pnumber = Integer.parseInt(split[0]);
        name = split[1];
        age = Integer.parseInt(split[2]);
        return new Person(name, age, pnumber);
    }
}
 while (scanner.hasNextLine()) {
       String line = scanner.nextLine();
       if(line.startsWith('#')) {
           // don't split the
       }
       else {
           // you can split
       }
  }
  scanner.close();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top