• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Tokenizer

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
https://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!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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



 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This work fine i got this.
if we use like this.

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about ...split("\\|{4}"); ?
 
Muddassir Ahmad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes bro it works also.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic