开发者

problem in reading text file

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:

readLoop:
while((strLine=br.readLine())!=null){

    // ...
    String[] splitVals = strLine.split(":");

    if((name.equals(nameUser))&&(pwd.equals(passWord))){
        System.out.println("welcome"+name);
        break readLoop;
    }

    // ...
}

Besides, I don't know why you need this loop:

for(int i=0;i<splitVals.length;i=i+2)

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:

System.out.printf("Username: %s, Password: %s%n", splitVals[0], splitVals[1]);

I would probably solve it using a Scanner. Something like this:

import java.io.*;
import java.util.Scanner;


public class TextFile {

    public static void main(String[] args) throws FileNotFoundException {

        if (userPassOk("hello", "world"))
            System.out.println("Welcome");
        else
            System.out.println("Get out!");
    }

    private static boolean userPassOk(String user, String pass)
            throws FileNotFoundException {

        Scanner s = new Scanner(new File("test.txt"));
        while (s.hasNextLine()) {
            String[] userPass = s.nextLine().split(":");
            if (userPass[0].equals(user) && userPass[1].equals(pass))
                return true;
        }
        return false;
    }
}


Try resetting the value of nameUser and passWord at the end of the try()

as in

nameUser=""; passWord="";


Your print statements are wrong. That's why you can't debug this properly.

What you are printing does not match what you are using for the name and password. Fix this and try printing it out again.

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();

However, you don't need this loop. You can just use:

        String nameUser=splitVals[0];
        String passWord=splitVals[1];


put break if your condition is satisfied.Don't allowed continue the loop.If you put the brak here.you got your expected output.

if((name.equals(nameUser))&&(pwd.equals(passWord))){
                System.out.println("welcome"+name);
                  break;
                }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜