wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Simple file processing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple file processing" Watch "Simple file processing" New topic
Author

Simple file processing

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30294
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30294
    
150

Here's the example I tested with. Maybe there is an extra character in yours that I don't have?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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:


The quieter you are, the more you are able to hear.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
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

Joined: Jun 19, 2010
Posts: 825
    
    5

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple file processing