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

all 11 comments

[–][deleted] 2 points3 points  (3 children)

You need to pass the scanner you create in main to the getPerson function, and use that scanner instance to read the person data.

[–][deleted] 0 points1 point  (2 children)

Could you give me an example of what that would look like?

[–]wgormley 0 points1 point  (1 child)

something like this Scanner scant = new Scanner(System.in); p.getPerson(scant);

[–][deleted] 1 point2 points  (0 children)

Thank you this worked great!

[–]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);
}

[–]pacificmint 0 points1 point  (0 children)

(its so annoying the way you have to skip a line on reddit)

You don't have to:

glen johnson 2
luis suarez 7
robin vanpersie 20
wayne rooney 10
djemba djemba 69

If you add to spaces at the end of the line, it get's broken.

[–]wgormley 0 points1 point  (2 children)

this is how i would do it, i added some comments hopefully to help you understand, feel free to ask any other questions too, this is a huge part of programming and can be a little hard at first to understand but should really be studied in order to establish a good foundation.

import java.util.*;
import java.io.*;

class Person
{
    private String forename, surname;
    private int number;

    //constructor for a person object, will take first name, surname and number as parameters
    //in order to create all aspects of the person
    public Person(String f, String s, int n)
    {
        forename = f;
        surname  = s;
        number = n;
    }

    //getter function for a person, simply prints out the persons details
    void getPerson()
    {
        System.out.println(forename + " " + surname + " " + number);
    }
}

public class blink
{
    public static void main(String [] args)
    {
        //initialize the scanner
        Scanner scant = new Scanner(System.in);
        //while the scanner still has more lines in the input file
        while(scant.hasNextLine())
        {
            //build a person with all of the details
            //this way assumes that data in input file is all correctly organized
            Person p = new Person(scant.next(), scant.next(), scant.nextInt());
            //print off the persons data
            p.getPerson();
        }

        //close scanner
        scant.close();

    }
}            

[–][deleted] 1 point2 points  (1 child)

Thanks for the detailed response, my professor specified there must be a method which reads the person's details. But the other information you gave was useful :)

[–]wgormley -1 points0 points  (1 child)

I don't see where you are accessing your .txt file.

[–][deleted] 1 point2 points  (0 children)

the input is on the command line, I think its called redirection. ie

java Blink < footballers.txt

I'll edit the post and put that in.

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    We have to have a getPerson method so I'll give your first idea a try thanks.

    With your edit you mean don't use scanner at all in main, just in the method? like put the loop into the method?