简体   繁体   中英

Searching a file based on given user-inputted filters and parsing input?

I am attempting to make a program that searches a given file for a Information is stored in the given format: Jane 19 50 86 85 84 83 45 76 There is the name, along with the age, followed by test scores. The user inputs a name and an age, then the program searches the file for a line that has this name and age. These test scores should be retrieved and stored, then printed. The program should then retrieve the information attached to that particular line in the file.

What I had planned on doing was storing the user input in Strings, then searching the file with these, like so:

    Scanner input = Scanner(System.in);
    String params = "";
    System.out.print("name? ");
    params += input.next() + " ";
    System.out.print("age? ");
    params += input.next();
    System.out.println(params);
    return params;  

But this seems as though it would cause complications. If I searched for a boy named Tom who was 19, would this not also match with a boy named Tommy who was 19?

In addition, I'm not really sure how to parse the numbers from the given line to print them. I made a separate Scanner for the input line, and attempted to use Integer.parseInt(), but the Scanner starts from the beginning of the line, so it attempts to change "Jane", for example, to a int, which is of course, not possible, so it creates a NumberFormatException.

Addition of relevant code:

public static String introQuery(Scanner input) {
    String params = "";
    System.out.print("name? ");
    params += input.next() + " ";
    System.out.print("age? ");
    params += input.next();
    System.out.println(params);
    return params;      
}
public static void fileNameScanner(Scanner file, String params) {
    boolean fileContains = false;
    System.out.println("fileNameScanner ran");
    int score = 0;
    Scanner split = new Scanner(params).useDelimiter(" ");
    String name = split.next().toUpperCase();
    String gender = split.next().toUpperCase();

    while (file.hasNextLine() /* && (fileContains = false)*/) {
        String inputLine = file.nextLine().toUpperCase();
        //commented out the test that would not work for the described case
        if (inputLine.contains(params))/*(inputLine.contains(name) && (inputLine.contains(age)))*/{ 
            Scanner numberSplit = new Scanner(inputLine);
            score = Integer.parseInt(numberSplit.next());
            fileContains = true;
        }
    }
    //this test does not work for some reason
    if (fileContains = false) {
        System.out.println("name/age combination not found");
    }

}

I would do it like this:

public static String[] getParams(Scanner input){
   String[] data = new String[2];
   System.out.print("Name? ");
   data[0] = input.nextLine();
   System.out.print("Age? ");
   data[1] = input.nextLine();
   System.out.println("Search parameters > Name: " + data[0] + "\tAge: " + data[1]);
   return data;
}

public static String searchFile(String filePath, String[] param) throws IOException, FileNotFoundException{
   BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
   List<String> fileData = new ArrayList<String>();
   List<String> NamesAndAge = new ArrayList<String>();
   String line;

   while((line = br.readLine()) != null){
      fileData.add(line);
   }
   br.close();
   for(String s : fileData){
      String[] split = s.split(" ");
      NamesAndAge.add(split[0] + " " + split[1]);
   }

   for(String n : NamesAndAge){
      if(n.equalsIgnoreCase(param[0] + " " + param[1])){
         int index = NamesAndAge.indexOf(n);
         return fileData.get(index).substring(n.length()).trim();
      }
   }
   return null;
}

The last method searchFile(String fileName, String[] param) will return the scores from the file eg. 1 4234 345 5646 13 . If the method returns null it didn't find a matching name and/or age.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM