从文本字段数据库中读取并不起作用

时间:2014-08-25 09:47:37

标签: java database

我创建了一个文本字段,用作用户名和密码的数据库。

我尝试制作一种方法来识别已经存在的用户名/密码是否相同,但它似乎不起作用。

我认为问题出在while循环条件(exists())中,但我无法找到解决方法。

public class Login{
    static Scanner s = new Scanner(System.in);
    static Scanner read;
    private static Formatter x;
    public static void main (String args[]){


        try{
            x = new Formatter("DataBase.txt");
        }catch(Exception e){
            e.printStackTrace();
        }
        try{
            read = new Scanner (new File("DataBase.txt"));
        }catch(Exception e){
            e.printStackTrace();
        }

        do{
            System.out.println("Type LOGIN to login, Type REGISTER to register");
            if (s.next().equals("REGISTER")){
                System.out.println("Insert username");
                String userName = s.next();
                while (exists(userName) == true){
                    System.out.println("ERROR! username already exists");
                    System.out.println("please selcet a different username");
                    userName = s.next();
                }System.out.println("username is legal");
                System.out.println("Insert password");
                String passWord =  s.next();
                while (exists(passWord)){
                    System.out.println("ERROR! password already exists");
                    System.out.println("please selcet a different password");
                    passWord = s.next();
                }
                x.format("%s %s"+System.lineSeparator(), userName,passWord);
                System.out.println("User created");
                System.out.println("Type continue to continue, or stop to close");
            }
        }while (s.next().equals("continue"));

        x.close();

    }

    public static Boolean exists(String str){
        while(read.hasNext()){
            System.out.println(read.next() +"," + str);
            if (read.next().equals(str)) {
                return true;
            }
        }
        return false;

    }

}

2 个答案:

答案 0 :(得分:0)

您正在阅读exists()功能中的2行。所以你有50%的机会获得-ver结果。

以下是更新后的代码:

public static Boolean exists(String str){
    while(read.hasNext()) {
        String line = read.next();
        System.out.println(line + "," + str);
        if (line.equals(str)) {
            return true;
        }
    }
    return false;
}

以上解决方案效率低下,您必须多次读取文件。相反,您可以创建cache并重复使用它。

像:

static Map<String, String> entires; // Create Cache

public static Boolean existsUser(String str) {
    if (entires == null) {
        entires = new HashMap<String, String>();
        while (read.hasNext()) {
            String[] line = read.next().trim().split(" ");
            entires.put(line[0], line[1]);
        }
    }

    return entires.containsKey(str);
}

public static Boolean existsPassword(String str, String user) {
    if (entires == null) {
        entires = new HashMap<String, String>();
        while (read.hasNext()) {
            String[] line = read.next().trim().split(" ");
            entires.put(line[0], line[1]);
        }
    }

    if (entires.containsKey(user)) {
        return entires.get(user).equals(str);
    }
    return false;
}

当然,您需要根据新条目不断更新cache,并且还需要重构代码才能使用这些方法。

答案 1 :(得分:0)

1)首先,您必须删除next()语句中对Syste.out..println的额外调用。

2)向Formatter添加数据后,您必须致电flush(),以便将数据写入DataBase.txt文件。

3)当文件中没有数据时,您的实例变量read正在开头打开DataBase.txt文件。因此,您的exists方法将始终返回false

4)Formatter.format方法未正确使用。

这是有效的代码:

public class Login {
    static Scanner s = new Scanner(System.in);
    static Scanner read;
    private static Formatter x;

    public static void main(String args[]) {    
        try {
            x = new Formatter("DataBase.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }

        do {
            System.out.println("Type LOGIN to login, Type REGISTER to register");
            if (s.next().equals("REGISTER")) {
                System.out.println("Insert username");
                String userName = s.next();
                while (exists(userName) == true) {
                    System.out.println("ERROR! username already exists");
                    System.out.println("please selcet a different username");
                    userName = s.next();
                }
                System.out.println("username is legal");
                System.out.println("Insert password");
                String passWord = s.next();
                while (exists(passWord)) {
                    System.out.println("ERROR! password already exists");
                    System.out.println("please selcet a different password");
                    passWord = s.next();
                }
                x.format("%s%s%s%s", userName,
                        System.getProperty("line.separator"), passWord,
                        System.getProperty("line.separator"));
                System.out.println("User created");
                System.out
                        .println("Type continue to continue, or stop to close");
                x.flush();
            }
        } while (s.next().equals("continue"));

        x.close();

    }

    public static Boolean exists(String str) {
        try {
            read = new Scanner(new File("DataBase.txt"));
            while (read.hasNext()) {
                String data = read.next();
                System.out.println(data);
                if (data.equals(str)) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            read.close();
        }
        return false;
    }

}
如Ambrish所述,您可以使用缓存来提高性能,而不是每次都从文件中读取数据。