• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple file processing

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this should be simple, and have done similar things to this before but for some reason this isn't working for me today.
I am just using a simple .txt file to hold some registrations to allow me to create a login function. I am reading the contents of the file line by line, and then reading each token, separated by a delimiter ","
The contents of the file looks like this.

Tom,password,
Bob,password,

There is no problem if i try to login with the first name and password. The problem occurs if i try and log in the second user. I have added a println to show me what values for username and password it is getting from the file and it says the correct username and password.
If i try and log in the second person i get this error, with line 165 being this.
String registeredPass = regIn.next();

Tompassword

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
Bobpassword
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at SimpleChat.Login(SimpleChat.java:165)

I believe the problem is to do with the password field and my delimiters as if i remove the password from the if statement it will log in using the username. I have tried many combinations of delimiters after i got this problem but cant find a solution yet

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are really close! Try it with while (regIn.hasNext()) instead of while (regIn.hasNextLine()).

The difference is that hasNext() is using your delimiter. With hasNextLine() you are mixing delimiters between commas and new lines. I'm not clear on why this fix works since there isn't another line. But it does. In practice, I try to stick to one delimiter at a time for my Scanners.

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You are really close! Try it with while (regIn.hasNext()) instead of while (regIn.hasNextLine()).

The difference is that hasNext() is using your delimiter. With hasNextLine() you are mixing delimiters between commas and new lines. I'm not clear on why this fix works since there isn't another line. But it does. In practice, I try to stick to one delimiter at a time for my Scanners.



Thanks for the reply. I have made that change, unfortunately its not worked for me.
Again, it is only when searching anything other than the first username and password.
The first println is the value read from the file for username and password.
The second line is the value entered by the user when trying to log in.

Chrispassword
Tompassword
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException

Tompassword
Tompassword
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at SimpleChat.Login(SimpleChat.java:182)

I have tried adjusting the delimiters in the file if i use this format ,Tom,password with the comma before and not after, i get no errors but this will not log in either user.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the example I tested with. Maybe there is an extra character in yours that I don't have?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you put only:

in your while loop and debug the code step by step, you can see exactly what's causing the problem.

With the file content:

those prints above will give you this:


However, the original file content:

would give:


Still, I think there is no need to make this so complicated. With the previous file format (username, password), this code is rather simple and does the job well:
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Jeanne and Kemal! I have it working now, i am using the idea Kemal gave. This is only for demonstration purposes, so this will be good enough to test my model.

Thanks again.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic