readLine() returns string type....

i have a file of usernames and passwords and am trying to check the username as when entered into login page.....

InputStreamReader i1=new InputStreamReader(new FileInputStream("usr"));
BufferedReader b=new BufferedReader(i1);

String str;
str=b.readLine();

for(int i=0;i<str.length;i++)
if(str[i]==("USERNAME: "+Login.usr))
{
bool=true;
}

now i know im trying to use a for loop using str which im unable to do so...

so what else can i do? ? ?

theres another method of using split() using it splitting into a string[]

but i dont want to use that.....

what else shud i do? ??

Recommended Answers

All 7 Replies

First fix this basic problem.
To compare two Strings for the same content you can't use ==, that just tests that they are exactly the same object. You need string1.equals(string2)

hey thanks for the suggestion i did that....

when i use String str[]=b.readLine();

it gives incompatible type error

how to go about it

Time to engage brain.
readline returns a String. An array of Strings isn't the same thing as a String, they are "incompatible types"
Did you want to have an array of Strings, each of which contains one line from your file? If so you need to have a loop in which you read the lines one at a time and put them into consecutive elements of the array.

hmmmm...an how do i read the lines one at a time from my file and put them into an array.....????

i dont want to use the split function.....and readline returns a string,not an array of strings....am loosin it..please help me out! ! !

you need to have a loop in which you read the lines one at a time and put them into consecutive elements of the array.

Try.

hey thanks for givin me the hint..i got it to work!!

InputStreamReader i1=new InputStreamReader(new FileInputStream("usr"));
BufferedReader b=new BufferedReader(i1);

String str;
while((str=b.readLine())!=null)
{
//System.out.println(str);
//bool=true;
if(str.equals("USERNAME: "+Login.usr))
{
bool=true;
break;
}
else
{
bool=false;
}

}

Hey that's great. I'm not saying that that's the best possible way to code that, but getting it working is a bloody good start. Here's another hint:
Put that code in its own method, eg something like
public boolean loginIsValid(String usr) {...
That way you can leave it for now while you get on with the rest, and you can always come back to improve it later without breaking anything else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.