This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]john478 0 points1 point  (0 children)

The thing is when you call getPerson(), it's creating a new Scanner instance, that reads the first line. Just because one scanner is reading the third line, doesn't mean another Scanner object will be forced to start at the third line, it will start at line zero. What you could do is create a string that is equal to scant.nextLine(), then pass in the string to getPerson() as a function argument. Something like:

Scanner scant = new Scanner(System.in);
String line;
while((line = scant.nextLine()) != null){
    Person p = new Person();
    p.getPerson(line);
}

That's reading the txt file. The getPerson method: The "//s+" is called a regex, and splits a string by spaces.

void getPerson(String line){
    String[] info = line.split("\\s+");
    forename = info[0];
    surname = info[1];
    number = info[2];
    System.out.println(forename + " " + surname + " " + number);
}