Results 1 to 2 of 2
- 03-25-2011, 06:26 AM #1Member
- Join Date
- Mar 2011
- Rep Power
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
Last edited by Parkuss; 03-25-2011 at 06:30 AM.
- 03-25-2011, 07:33 AM #2Senior Member
- Join Date
- Jun 2008
- Rep Power
Well, make sure that there is a "nextElement" before calling next. Or better yet, if you're simply comparing file contents, use bufferedreader, readline, split (if extra whitespace is irrelevant, or replace if all whitespace is irrelevant, except for newlines in both cases, otherwise you need to read the entire file at once, or use a much more complicated algorithm), and two loops.
- By dude1it in forum New To JavaReplies: 6Last Post: 03-11-2011, 03:53 AM
- By ariz in forum New To JavaReplies: 5Last Post: 02-27-2009, 05:19 AM
- By vileoxidation in forum New To JavaReplies: 5Last Post: 09-17-2008, 07:29 AM
- By ragav in forum New To JavaReplies: 4Last Post: 06-08-2008, 02:19 PM
- By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM