Solved

Java search string

Posted on 2006-10-25
4
205 Views
Last Modified: 2010-03-31
Hi!
I have a text file with username and password.
They are in the format username:password

When the user enters a new username to create, i would like to do a search in this text file to see if such a username has been taken up.
If it has taken up, to return a boolean true;
Please advice.

Cheers!
Darence

This is currently what have.
Please advice.

Boolean check = false;
FileInputStream fstream = new FileInputStream("test.txt");
DataInputStream checkUserName = new DataInputStream(fstream);
                              
while (checkUserName.available() !=0){
System.out.println (checkUserName.readLine());
0
Comment
Question by:darenceang
  • 3
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17801723
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

Why would it matter if just the username were taken? You need to check the name/password *pairs*
0
 
LVL 4

Author Comment

by:darenceang
ID: 17801740
If the username is taken, then the username will not be created.
The system is like a user/password creation.

So if i enter an option to create user : darenceang

The system will check this txt file to see if darenceang has been registered before.
Else the system will prompt him for a password and then save it into the file as username:password.

I m stuck at the checking of username ( string ) from the inputstream.

Cheers!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 17801765
Place

boolean userFound = false;

before the code at that link i posted then replace the loop code with

     while ((str = in.readLine()) != null && !userFound) {
            userFound = str.startsWith(username);
     }


.............

     return userFound;

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17801817
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now