Martin Williams
Martin Williams

Reputation: 21

How would I change this code to make it so that it asks me the location of the file that I want to load?

So my code currently has the user specify the name of the file that they want to load within the code itself but how would I make it so that when the program is run then the user will enter the location of the file that they want to load?

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

public class reader {

    static int validresults = 0;
    static int invalidresults = 0;
    //used to count the number of invalid and valid matches

    public static boolean verifyFormat(String[] words) {
        boolean valid = true;
        if (words.length != 4) { 
            valid = false;
        } else if (words[0].isEmpty() || words[0].matches("\\s+")) {
            valid = false;
        } else if ( words[1].isEmpty() || words[1].matches("\\s+")) {
            valid = false;
        }

        return valid && isInteger(words[2]) && isInteger(words[3]);}

    //checks to see that the number of items in the file are equal to the four needed and the last 2 are integers
    //also checks to make sure that there are no results that are just whitespace

    public static boolean isInteger( String input ) {
        try {
            Integer.parseInt( input );
            return true;
        }
        catch( Exception e ) {
            return false;
        }
    }
    //checks to make sure that the data is an integer

    public static void main(String[] args) throws FileNotFoundException {

        String hteam;
        String ateam;
        int hscore;
        int ascore;
        int totgoals = 0;

        Scanner s = new Scanner(new BufferedReader(
                new FileReader("fbscores.txt"))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");

        while (s.hasNext()) {
            String line = s.nextLine();
            String[] words = line.split("\\s*:\\s*");
            //splits the file at colons

            if(verifyFormat(words)) {
                hteam = words[0];       // read the home team
                ateam = words[1];       // read the away team
                hscore = Integer.parseInt(words[2]);       //read the home team score
                totgoals = totgoals + hscore;
                ascore = Integer.parseInt(words[3]);       //read the away team score
                totgoals = totgoals + ascore;
                validresults = validresults + 1;

                System.out.println(hteam + " " +  "[" + hscore + "]" +  " " + ateam + " " + "[" + ascore + "]");   
                //output the data from the file in the format requested

            }
            else{
                invalidresults = invalidresults + 1;
            }
        }
    System.out.println("Total number of goals scored was " + totgoals);
    //displays the the total number of goals
    System.out.println("Valid number of games is " + validresults);
    System.out.println("Invalid number of games is " + invalidresults);

        System.out.println("EOF");
    }
}

Upvotes: 0

Views: 194

Answers (1)

nuala
nuala

Reputation: 2689

One approach would be to use a main loop asking for a file name and quitting program execution when no input is given.

Therefore I'd refactor most code of your main method into another function e.g. processFile(String fileName).

Then your main only deals with user input

public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  while(true){ //keep running till we break

    System.out.println("Enter filename or return blank line to quit");
    String fileName = sc.nextLine();

    if(fileName != null && !fileName.isEmpty()){ 
      processFile(fileName)
    }else{
      break; //no user input => exit
    }
  }
  System.out.println("bye");
}

private static processFile(String fileName){
    String hteam;
    String ateam;
    int hscore;
    int ascore;
    int totgoals = 0;

    Scanner s = new Scanner(new BufferedReader(
            new FileReader(fileName))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");

    while (s.hasNext()) {
      … //rest of your original code
}

Upvotes: 1

Related Questions