Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Returning Scanner objects (http://www.go4expert.com/forums/returning-scanner-objects-t19491/)

a1dogtraining 20Sep2009 08:06

Returning Scanner objects
I don't know how to return a scanner object. See below a portion of my 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: ");
name = scan.nextLine();
if (name == "student list")
name = "slist";
Scanner fin = new Scanner (new File(name));
catch (FileNotFoundException e){
System.out.println("File not found try again:");


The return statement above is my problem

All times are GMT +5.5. The time now is 15:49.