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 :)