• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Help please with input errors  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote an program allow users to register for an account. I get this error when running the program:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Register.main(Register.java:54)

Help would be very well appreciated.
Here is the code:

 
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You call hasNext() once but next() twice. I'm guessing your file only has one  "word" saved in it. With the default delimiter a Scanner will determine what next() returns by detecting white space.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are opening the file for writing and then opening the file for reading without first closing the writer. You should do each operation separately and perform a close() before doing the next operation.
 
Ryan Harris
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You are opening the file for writing and then opening the file for reading without first closing the writer. You should do each operation separately and perform a close() before doing the next operation.


I'm still sure where in the code to fix the problem.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opening a Scanner to System.in gives you access to keyboard input. Normally keyboard input is retrieved using nextLine() so that, A) spaces can be included in the entry, and B) input is not left in the buffer when you get the next keyboard input.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Harris wrote:

Carey Brown wrote:You are opening the file for writing and then opening the file for reading without first closing the writer. You should do each operation separately and perform a close() before doing the next operation.


I'm still sure where in the code to fix the problem.


It looks like you don't need to open the file on line 45.
 
Ryan Harris
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Opening a Scanner to System.in gives you access to keyboard input. Normally keyboard input is retrieved using nextLine() so that, A) spaces can be included in the entry, and B) input is not left in the buffer when you get the next keyboard input.


Could you post an example? I new to java here.
 
Ryan Harris
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added " sc.close() "  before writing to the file, but still have the same error.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:It looks like you don't need to open the file on line 45.


Line 49 says "store usernames". You are not storing you are reading. When you're done reading you should immediately close the Scanner.

When you read the file you are only reading 2 fields but when you write the file you are writing 3.
 
Ryan Harris
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:It looks like you don't need to open the file on line 45.


Line 49 says "store usernames". You are not storing you are reading. When you're done reading you should immediately close the Scanner.

When you read the file you are only reading 2 fields but when you write the file you are writing 3.



Ok, I have removed this line of code from line 45 " FileWriter object = new FileWriter("database.txt", true);". I still get the same error.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Harris wrote:I added " sc.close() "  before writing to the file, but still have the same error.


  • "sc" has nothing to do with your file I/O. This wasn't a problem but now it is.
  • A Scanner created from System.in (i.e. your sc Scanner), should NEVER, NEVER be closed. That permanently closes the programs input stream which can never be reopened while the program is still executing. "new Scanner(System.in);" should only happen once inside a program.
  •  
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Ryan Harris wrote:I added " sc.close() "  before writing to the file, but still have the same error.


  • "sc" has nothing to do with your file I/O. This wasn't a problem but now it is.
  • A Scanner created from System.in (i.e. your sc Scanner), should NEVER, NEVER be closed. That permanently closes the programs input stream which can never be reopened while the program is still executing. "new Scanner(System.in);" should only happen once inside a program.

  • " sc.close() " has been removed.
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ryan Harris wrote:Ok, I have removed this line of code from line 45 " FileWriter object = new FileWriter("database.txt", true);". I still get the same error.


    I'd need to see the file (or a replica of it). Cut and paste the contents in your next post as well as the code as it stands now.
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Ryan Harris wrote:Ok, I have removed this line of code from line 45 " FileWriter object = new FileWriter("database.txt", true);". I still get the same error.


    I'd need to see the file (or a replica of it). Cut and paste the contents in your next post as well as the code as it stands now.



    Here is what it printed to the database file: Ryan 8b0e488bdc218700c3018496e0f7efb17c79f415ec392b166df3a931984c2c108df2cdfe3c37f52a891fe390d9950f057a107b24b8a72b24c61a84ebf724bfc2 Harris
    Wildcat 0e4e80a1602a5c87b3a7b50bda046d1a08596c529ac8361fcf871b40c071677cba600e0fefa310b4e0c5c6ea819ea048536181ff2e3e6452f4af7d5ad824cfd1 6000
    Dr. 1604949526cd0188f7f6a13c0c6398e79bceffc296eef733c63692813a1fd991c8391bcdecfda0d4477cd9b98a0e1fbb318862346d70763f99ee1ca0385ee77c Log

    Here is the code itself:

     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your code formatting needs some work, primarily the indenting.

    I ran what you gave me through an Eclipse formatter and this is the result. One difference you'll note is the placement of the braces with the open brace '{' appearing directly above the closed brace '}'. This is called the Alman style and is what I suggest to beginners; it's easier to see misplaced braces. Proper formatting is not just for appearance, it improves readability and points out some potential bugs you may have missed.


     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This will read 2 fields but you actually have 3. What about the plain text password?
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:This will read 2 fields but you actually have 3. What about the plain text password?


    How do I fix it?
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ryan Harris wrote:

    Carey Brown wrote:This will read 2 fields but you actually have 3. What about the plain text password?


    How do I fix it?


    Do it the same way you're doing user and hash.
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Ryan Harris wrote:

    Carey Brown wrote:This will read 2 fields but you actually have 3. What about the plain text password?


    How do I fix it?


    Do it the same way you're doing user and hash.



    This still does not fix the exception error I am receiving.
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Your code formatting needs some work, primarily the indenting.

    I ran what you gave me through an Eclipse formatter and this is the result. One difference you'll note is the placement of the braces with the open brace '{' appearing directly above the closed brace '}'. This is called the Alman style and is what I suggest to beginners; it's easier to see misplaced braces. Proper formatting is not just for appearance, it improves readability and points out some potential bugs you may have missed.




    Any reason while the main method is now missing as it is required to run the file?
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Carey Brown wrote:Oops, main( String[] args ) is missing the '[]'.


    I added the " [] " but still having the same error I started off with.
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you're talking about the read error then show me the changes you made to add the reading of the plain-text password.
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:If you're talking about the read error then show me the changes you made to add the reading of the plain-text password.


    This error: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at Register.main(Register.java:54)
    from the post above
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:This will read 2 fields but you actually have 3. What about the plain text password?


    So? What did you do to fix this?
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Carey Brown wrote:This will read 2 fields but you actually have 3. What about the plain text password?


    So? What did you do to fix this?


    This is what I need help in fixing.
    But here is what I tried:
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ryan Harris wrote:

    Carey Brown wrote:

    Carey Brown wrote:This will read 2 fields but you actually have 3. What about the plain text password?


    So? What did you do to fix this?


    This is what I need help in fixing.
    But here is what I tried:



    Also I changed the name of the class to register, to be a bit more organized
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your writing to the file used to be ok, now it is broken. You didn't need to change this you only needed to read the plain-text field in your read code. Now they're both broken.

    Do you need plain text in your file or not?

    After every change you should delete the .txt file. If you have entries in the file made with two different iterations of code who knows if the data is formatted correctly.
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Your writing to the file used to be ok, now it is broken. You didn't need to change this you only needed to read the plain-text field in your read code. Now they're both broken.

    Do you need plain text in your file or not?

    After every change you should delete the .txt file. If you have entries in the file made with two different iterations of code who knows if the data is formatted correctly.


    Yes plaintext text is needed, an requirement of the program
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Plain-text is no longer here.
     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:
    Plain-text is no longer here.



     
    Ryan Harris
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Now 'user' is missing. The devil's in the details.


    See exactly, I dont know what im doing here
     
    Carey Brown
    Bartender
    Posts: 5775
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    This is your original code. It is correct.

    In your read loop:

    You need to add another call to sc1.next() to read the third field. You can throw away what this call returns, it only matters that you read it.

    In your file you have
    U \t H \t P \n
    U \t H \t P \n

    etc
    (U=user, H=hash, P=password)

    So, if you only read two you'll get U and H, which leaves P in the queue. The next time around the loop you'll get P and U, which is wrong. Scanner (by default) treats a space, a tab, and a new-line exactly the same. So if you don't read all 3 then you'll have 1 field left over which will be read in your next time around the loop.

    BTW, this could be an issue if you permit user names and passwords to contain spaces.
     
    It's exactly the same and completely different as this tiny ad:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!