Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Tokenizer

 
Alexandros Mittos
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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



 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This work fine i got this.
if we use like this.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about ...split("\\|{4}"); ?
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes bro it works also.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic