hello I am new to java and am having trouble with this simple file read and compare program.
Basically my issue is that I cannot seem to get this method to return a false value. I can get the program to read and compare and return true but if I enter a input that does not exist in the file it throws this exception.
"Exception in thread "main" java.util.NoSuchElementException
here is my code for my method
public static boolean checkUsername(String uName, String pWord, String filename)
File file = new File(filename);
Scanner input = null;
input = new Scanner(file);
uName1 = input.next();
pWord1 = input.next();
if (uName1.equals(uName) && pWord1.equals(pWord))
System.out.println("file does not exist");
if (input != null)
The exception falls at the uName1 in the while loop. I'm assuming that is only because it is the first comparison within the loop and it obviously doesnt match. Oh and when i pass the value to the method I pass the user input from the driver, hence the comparison from the uName1 to uName listed as a parameter. (i.e. uName is what the user enters from the driver program and what I am trying to do is compare this data to what already exists in the file).
I apologize if the code comes out messy and confusing once posted I know these windows have different character limits when posted. Any help would be appreciated