通过扫描仪验证输入数据

时间:2017-06-05 18:32:32

标签: java arrays string if-statement trim

我的if语句有问题。有一个包含 admin.admin 行的文件。我将线分成点并将其存储在数组中。数组的第一个元素是登录。数组的第一个元素与输入值(代码String inputLogin = textField[0].getText()中的inputLogin =“admin”)进行比较。我必须得到真实的,但我实际上得到了一个假的

第一个java文件

package classPackage;

import java.awt.*;

import javax.swing.*;

public class ClassAuthorization extends JFrame {

private static final int WIDTH_AUTH = 400;
private static final int HEIGHT_AUTH = 500;

private JLabel[] label = new JLabel[3];
public JTextField[] textField = new JTextField[1];
public JPasswordField[] passwordField = new JPasswordField[1];
protected JButton[] button = new JButton[2];

private String[] text = {"Авторизуйтесь", "Логин:", "Пароль:", "Вход", "Я - участник"};

private Integer[] coordXAuth = {110, 39, 25, 110, 110, 25, 25};
private Integer[] coordYAuth = {20, 120, 170, 125, 175, 225, 285};

private Integer[] widthAuth = {200, 100, 100, 220, 220, 305, 305};
private Integer[] heightAuth = {30, 30, 30, 27, 26, 40, 40};

private Integer[] sizeFont = {24, 22, 22, 18, 18, 18, 18};

public ClassAuthorization() {

    setSize(WIDTH_AUTH, HEIGHT_AUTH);
    setLocationRelativeTo(null);

    JPanel panel = new JPanel();
    getContentPane().setLayout(null);
    setPanel(panel, 10, 11, 400, 500);
    getContentPane().add(panel);

    for (int i = 0; i < 3; i++) {
        if (i < 1) {
            addLabel(i, panel);
            addTextField(i, panel);
            addPasswordField(i, panel);
            addButton(i, panel);
        }
        else if (i == 1) {
        addLabel(i, panel);
        addButton(i, panel);
        }
        else if (i > 1) {
            addLabel(i, panel);
        }
    }

}

private JPanel setPanel (JPanel panel, int x, int y, int width, int height) {   

    panel.setBounds(x, y, width, height);
    panel.setLayout(null);
    return panel;

}

private void addLabel(int i, JPanel panel) {

    label[i] = new JLabel();
    label[i].setText(text[i]);
    label[i].setBounds(coordXAuth[i], coordYAuth[i], widthAuth[i], heightAuth[i]);
    label[i].setFont(new Font("Segoe UI Light", Font.PLAIN, sizeFont[i]));
    panel.add(label[i]);

}

private void addTextField(int i, JPanel panel) {

    textField[i] = new JTextField();
    textField[i].setBounds(coordXAuth[i + 3], coordYAuth[i + 3], widthAuth[i + 3], heightAuth[i + 3]);
    textField[i].setFont(new Font("Segoe UI Light", Font.PLAIN, sizeFont[i + 3]));
    panel.add(textField[i]);
}

private void addPasswordField(int i, JPanel panel) {

    passwordField[i] = new JPasswordField();
    passwordField[i].setBounds(coordXAuth[i + 4], coordYAuth[i + 4], widthAuth[i + 4], heightAuth[i + 4]);
    panel.add(passwordField[i]);

}

private void addButton(int i, JPanel panel) {

    button[i] = new JButton();
    button[i].setText(text[i + 3]);
    button[i].setBounds(coordXAuth[i + 5], coordYAuth[i + 5], widthAuth[i + 5], heightAuth[i + 5]);
    button[i].setFont(new Font("Segoe UI Light", Font.PLAIN, sizeFont[i + 5]));
    panel.add(button[i]);
}

}

第二个java文件

package mainPackage;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JButton;

import classPackage.ClassAuthorization;

public class Authorization extends ClassAuthorization {

private String path = "src/putFile/Account.txt";

public Authorization() {
    checkAccess(button[0]);
}

private void checkAccess(JButton button) {

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            File file = new File(path);
            Scanner scanner;

            try {

                scanner = new Scanner(file);

                do {

                    String account = scanner.nextLine();
                    String[] parts = account.split("\\.");
                    String login = parts[0].trim();
                    String pass = parts[1];
                    char [] password = pass.toCharArray();
                    String inputLogin = textField[0].getText();
                    char[] inputPassword = passwordField[0].getPassword();
                    System.out.println(inputPassword);

                    if (inputLogin.equals(login) == true && Arrays.equals(password, inputPassword) == true) { // BAD

                         System.out.println("GOOD");

                        break; 
                    }
                    else {

                        System.out.println("BAD");
                    }
                } while (scanner.hasNextLine() == true);
            }
            catch (FileNotFoundException NotFoundFile) {
                NotFoundFile.printStackTrace();
            }
        }
    });
}


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Authorization frame = new Authorization();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


}

Account.txt

admin.admin

1 个答案:

答案 0 :(得分:2)

在我真正回答你的问题之前,我必须提出一些建议:

  1. 避免扩展JFrame,因为JFrame是一个严格的容器,而是为JPanel构建GUI并实例化JFrame,请在以下位置获取答案: Extends JFrame vs. creating it inside the program作为参考

  2. 为什么要为这些行创建一个完整的数组:

    public JTextField[] textField = new JTextField[1];
    public JPasswordField[] passwordField = new JPasswordField[1];
    

    如果您要使用单个元素,则不需要数组。

  3. 以下几行对我没有意义,因为当你可以使用原语Integer时,不需要将数字包装为int,这会影响程序的性能。

    private Integer[] coordXAuth = {110, 39, 25, 110, 110, 25, 25};
    private Integer[] coordYAuth = {20, 120, 170, 125, 175, 225, 285};
    
  4. getContentPane().setLayout(null); 停止使用null-layout!请仔细阅读如何使用不同的layout managers,您可以将它们组合起来并创建很酷的GUI。

    Swing旨在使用它们,因为它必须处理不同的OS,PLAF,屏幕尺寸和分辨率,像素完美的GUI是一种幻觉,here是使用它们时会发生什么的一个例子。 / p>

    有关详细信息,请参阅:Why is it frowned upon to use a null layout in Swing?What's wrong with the Null Layout in Java?Null layout is evil

    立即停止使用setLayout(null)setBounds(...)setLocation(...)方法!

  5. 这些行:

    while (scanner.hasNextLine() == true);
    if (inputLogin.equals(login) == true && Arrays.equals(password, inputPassword) == true) { // BAD
    

    可能是这样写的:

    while (scanner.hasNextLine()); //Removed == true
    if (inputLogin.equals(login) && Arrays.equals(password, inputPassword)) { //Removed == true
    

    因为这些方法已经返回一个布尔值,所以,没有必要将布尔值与另一个布尔值进行比较,这与if (true == true)相同,为什么不是if (true)?它更容易阅读和理解。

  6. 但是,我必须祝贺你把你的节目放在了EDT上。

    现在,在我们说完上述建议之后,让我们继续讨论该计划......它对我来说很好:

    这是我运行它时的控制台输出:

    admin
    GOOD
    

    所以,问题可能在你的文件上,我建议你再写一次吗?

    以下是使用以下方法生成类似输出的示例:

    • 外部窗格的GridBagLayout
    • 用户/密码标签和字段窗格<{1}}

    代码:

    GridLayout

    正如您所看到的,代码很短,它是完整的,因为您可以复制粘贴它并通过单个修改(文件路径)查看输出,它是可验证的(它显示与下面相同的输出),这就是你的当你被要求做MCVE时,我希望这样做。没有发布相关代码,好吧,我无法写出前五个建议:)

    我的输出与你的输出,两者都非常相似,只是更改字体,它应该看起来像之前......

    enter image description here enter image description here

相关问题