Results 1 to 2 of 2
  1. #1
    Parkuss is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default java.util.NoSuchElementException

    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
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at WoWaccount.checkUsername(WoWaccount.java:139)
    at TestWoW.main(TestWoW.java:80)
    "

    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;
    String uName1;
    String pWord1;
    try
    {
    input = new Scanner(file);
    while(input.hasNextLine())
    {
    uName1 = input.next();
    pWord1 = input.next();

    if (uName1.equals(uName) && pWord1.equals(pWord))

    return true;
    }

    return false;
    }
    catch(FileNotFoundException e)
    {
    System.out.println("file does not exist");
    }
    finally
    {
    if (input != null)
    input.close();
    }

    return false;

    }

    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
    Thanks
    Last edited by Parkuss; 03-25-2011 at 06:30 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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.

Similar Threads

  1. Replies: 6
    Last Post: 03-11-2011, 03:53 AM
  2. Replies: 5
    Last Post: 02-27-2009, 05:19 AM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •