I have a problem with if-statement. There is a file with the line admin.admin. I break the line into points and store it in an array. The first element of the array is the login. The first element of the array is compared with the input value (inputLogin = "admin" in the code String inputLogin = textField[0].getText()
). I have to get the true, but I'm actually getting a false
first java file
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]);
}
}
Second java file
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