0

I have a .txt file in here "C:\failass.txt". It contains the following info:

AA051245445454552117989
LT647044001231465456
LT517044077788877777
LT227044077788877777
CC051245445454552117989

I have found a code which scans if the user input matches the info contained in the file:

package ibanas;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class ReadFromFile {

public static void main(String[] args) throws IOException {
    Scanner myScanner = new Scanner(System.in);
    System.out.println("What number would you like to check for?");
    String number = myScanner.nextLine();
    if(isFound(number)){
        System.out.println("The number "+number+ " is there");
    }else{
        System.out.println("The number "+number+ " doesn't exist");
    }
}

public static boolean isFound(String number) {

    Scanner sc = new   Scanner(ReadFromFile.class.getResourceAsStream("C:\failass.txt"));
    String word="";
    while (sc.hasNextLine()) {
        word = sc.next();

        if (word.equals(number.trim())) { 
            return true;
        }
    }
    return false;
}
}

But when i enter (for example) this line AA051245445454552117989 it gives me the following error:

What number would you like to check for?
AA051245445454552117989
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at ibanas.ReadFromFile.isFound(ReadFromFile.java:24)
at ibanas.ReadFromFile.main(ReadFromFile.java:15)

Thanks for any advice :)

  • You can put the `failass.txt` file in the same folder as the `ReadFromFile` class and change the code to Scanner sc = new Scanner(ReadFromFile.class.getResourceAsStream("failass.txt")); – wake-0 Jul 13 '16 at 07:23
  • @KevinWallis thanks it works now with all the numbers in the txt file except for the first one AA051245445454552117989. Gives the same error. Maybe there is something wrong with the string or what? – M. Cherbex Jul 13 '16 at 07:30
  • Do have any kind of whitespace there? – wake-0 Jul 13 '16 at 07:30
  • @KevinWallis no it doesn't. Seems strange.. because i tried to put this number below, replaced it with the number LT647044001231465456 and now it shows error with this one. I think there is a problem reading the first line of txt file... – M. Cherbex Jul 13 '16 at 07:52
  • I created the file by my own with the data from the question above and it worked. Have you debugged ? – wake-0 Jul 13 '16 at 07:54
  • @KevinWallis yes i have debugged, if you mean just by clicking the F11 button, but this doesn't seem to work. I will try to study about NullPointerException, maybe that's the clue. – M. Cherbex Jul 13 '16 at 11:28

1 Answers1

0

the arg for the scanner, namely ReadFromFile.class.getResourceAsStream("C:\failass.txt") returns null. probably because you need to escape the backslash with another "C:\\failass.txt"

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47