mercredi 9 mai 2018

Java - using scanner to return a line from text file if line contains scanner input

I am wanting to store a line from a text file depending on if the input from a scanner is contained within the line of text.

However, the code below accepts all form of input, regardless of if the input is contained in the text file, so I am assuming the if else statement is completely ignored. I am unsure on how to fix this so any advice is appreciated.

Scanner editScanner = new Scanner(System.in);
String editScannerInput = editScanner.nextLine();
Scanner fileScanner = new Scanner(new File("src/FilmInfo.dat"));
String fileScannerLine = fileScanner.nextLine();


while (fileScanner.hasNext()) {
                    if (fileScannerLine.contains(editScannerInput.toLowerCase())) {
                        String chosenFilm = fileScanner.nextLine();
                        System.out.println(chosenFilm);
                    } else {
                        System.out.println("Film name is not recognised in your collection, please enter a film name that is in your collection.");
                    }

Aucun commentaire:

Enregistrer un commentaire