aspose file tools*
The moose likes Beginning Java and the fly likes Problem with Tokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Tokenizer" Watch "Problem with Tokenizer" New topic
Author

Problem with Tokenizer

Alexandros Mittos
Greenhorn

Joined: Oct 22, 2010
Posts: 1
Hello, I'm new in Java and I have a problem with Tokenizer.

I'm writing a program which the user enters his username and password and the program checks if that username and password exits. The user database is in a text file called "userlist.txt". The file looks like this:

user1:42048
user2:89342
user3:90342


Where in the first pane column is the username and in the second the password (obviously! ). I know how to separate the columns (relatively easy), here's the code:



That piece of code produces this:

:
user1 42048
user2 89342
user3 90342


Now here's the problem. I want the user to type his/her username and the program will check if that username exists (in this case, the possible usernames are user1, user2 or user3). And if the username exists the program will ask the password and checks if that's correct. And I have no idea how to check certain words in a tokenized file. I know it has something to do with nextToken() but I can't figure it out. What worries me the most is that the program will not search the whole file, but instead, it will search in which line the username exists and then will check the password.

Any ideas or something to point me to the right direction? I'm stuck.

Thank you in advance.


"Somewhere, something incredible is waiting to be known." - Carl Sagan
"Ah, I love your religion - for the crazy! Virgin birth, water into wine; it's like Harry Potter, but it causes genocide and bad folk music." - Roger (Seth McFarlane)
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
How about having a map with Strings as both key and value. You can add all the userids and passwords as key-value pairs to the map.

Then check if the userId entered is in the map's keys and get the corresponding value(password).
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
Hi Alex.

From th JDK6 API:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

It's actually been this way for quite some time now as I was reminded of back in 2006:
http://www.coderanch.com/t/379978/java/java/String

Other than that, using a map as Sridhar suggested (maybe as a Singleton) sounds like a good solution.

Oh, and WELCOME!
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
hey i have a problem. I am splitting a string like
12||pizza||||13||jeniffer||||14||xyz||||

I am using



output : its output is 12, pizza, 13, jeniffer, 14, xyz (means having array of size 6)

instead of : 12||pizza, 13||jeniffer, 14||xyz (means array having size 3 which is required)

I m reallli ocnfused. please help.

hey i will be working hard to find out people's problems
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

String.split uses regular expressions, and | has a special meaning in regexes. In short, your splitting criteria is "nothing, or nothing, or nothing, or nothing, or nothing".

Unless you didn't PostRealCode, which you probably didn't, as I see at least one compiler error (split returns a String[], not a String).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
Sorry Rob for my mistakes. Was in hurry. I am sending you example code.



out put is:

a array with size of 2.
at 0 index 2
at 1 index pizza

Thanks for your quick reply.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're lying. Put bluntly, but it's true. That code does not produce an array with elements "2" and "Pizza". It produces an array with elements "", "2", "|", "|", "P", "i", "z", "z", "a", "|", "|", "|" and "|", and the reason can be found in my previous post - you split at "nothing", so each element becomes its own array element, including the leading empty string. Your output can be created using a StringTokenizer(splittable, "||||").
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10


out put is:
0 index is : 2
1 index is : pizza



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

But that's not using String.split, which you said you were using.

OK, the reason for your problem. StringTokenizer is garbage. Eh, I mean, StringTokenizer uses characters for splitting. Not sequences of characters. There is no difference in behaviour between new StringTokenizer(splittable, "||||") and new StringTokenizer(splittable, "|"). In both cases it still uses only | to split. Multiple occurrences of | will be regarded as one separator.

Using String.split (for real this time) you can solve this, but remember to not use splittable.split("||||"), for reasons I already mentioned. You must escape those pipe symbols.
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
This work fine i got this.
if we use like this.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38888
    
  23
What about ...split("\\|{4}"); ?
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
yes bro it works also.
 
 
subject: Problem with Tokenizer