1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Returning Scanner objects

Discussion in 'Java' started by a1dogtraining, Sep 20, 2009.

  1. a1dogtraining

    a1dogtraining New Member

    Joined:
    May 20, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Training Dogs, Student
    Location:
    Banora Point NSW
    I don't know how to return a scanner object. See below a portion of my code.

    CODE

    public class MarkEntry {

    public final int LEN = 200;

    private String[] studNos; // Array for stroing student No's or ID's
    private int[] studScores; // Array for storing the results
    private int nStudents;

    Scanner console = new Scanner(System.in);
    public MarkEntry() {

    int[] studScores = new int [LEN];
    String[] studNos = new String [LEN];
    nStudents = 0;

    }

    // This method repeatedly prompts user to enter a filename until user enters a valid filename.
    // Once a valid filename is entered, this method returns a Scanner object created from the file.
    // The parameter msg is used to construct the propmpt message by: "Enter " + msg + "filename: ".
    // So if msg contains the string student list, then the prompt message would be: ?Enter student list filename:

    Scanner getFileName(String msg)throws FileNotFoundException {


    boolean valid = false;
    Scanner scan = new Scanner (System.in);

    String name = "";
    while (true)
    {
    try {
    System.out.print ("Enter file name: ");
    System.out.flush();
    name = scan.nextLine();
    if (name == "student list")
    name = "slist";
    Scanner fin = new Scanner (new File(name));
    break;
    }
    catch (FileNotFoundException e){
    System.out.println("File not found try again:");
    }

    return
    }
    END CODE

    The return statement above is my problem
     

Share This Page