I have a text file where i have names and passwords separated by :.
user1:pwd1
user2:pwd2
In the login page if the user gives the correct username and password it will lead you to the welcome page. But I am not getting this properly. The output which i get is
user1
pwd1
inside try
user1
pwd1
true
welcome user1
user2
pwd2
false
not equal
My code is below.
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.regex.*;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
public class TextFile {
/**
* @param args
*/
public void getNamePwd(String name, String pwd) {
// TODO Auto-generated method stub
System.out.println(name);
System.out.println(pwd);
String[] splitVals=null;
try{
System.out.println("inside try");
String strLine;
BufferedReader br = new BufferedReader(new FileReader("D:\\test\\text.txt"));
while((strLine=br.readLine())!=null){
splitVals=strLine.split(":");
for(int i=0;i<splitVals.length;i=i+2){
System.out.println(splitVals[i].toString());
System.out.println(splitVals[i].toString());
String nameUser=splitVals[i].toString();
String passWord=splitVals[i+1].toString();
System.out.println(name.equals(nameUser));
if((name.equals(nameUser))&&(pwd.equals(passWord))){
System.out.println("welcome"+name);
}
else{
System.out.println("not equal");
}
}
}
}catch(Exception e){
}
}
}
please help me..
I suspect that you want to stop looking for username/password matches after you’ve found one… To do this you have to break the loop upon a match. To do this you do the following:
Besides, I don’t know why you need this loop:
Recall that you read the file line by line. That is, the splitted array will contain the username and password of the current line.
To print the username / password you could do something like this:
I would probably solve it using a
Scanner. Something like this: