我的应用程序遇到了一些麻烦。
在我的validate
函数中,即使a
和m
相同,b
和n
也一样,它仍会返回false。
仍然在validate
函数中,ArrayList<staff> st = aa.getst();
调用返回一个空列表。
职员:
import java.util.ArrayList;
public class staff {
private String StaffID;
private String StaffPW;
private ArrayList<staff> st;
public staff (String StaffID, String StaffPW) {
st = new ArrayList<staff>();
this.StaffID = StaffID;
this.StaffPW = StaffPW;
}
public void thestaff(){
staff d = new staff("Chan","123");
staff e = new staff("Wong","456");
staff f = new staff("Fong","789");
st.add (d);
st.add (e);
st.add (f);
}
public String getID() {
return StaffID;
}
public String getPW() {
return StaffPW;
}
public ArrayList<staff> getst() {
return st;
}
}
&#39;
登录
import java.util.ArrayList;
public class Login {
private static staff aa;
private static String c,d;
public static boolean validate(String a, String b) {
aa = new staff(c,d);
ArrayList<staff> st = aa.getst();
System.out.println (st.size());
for (int i = 0; i < st.size(); i++) {
String m = st.get(i).getID();
String n = st.get(i).getPW();
if (a == m) and (b==n)
return true;
}
return false;
}
}
&#39;
CreateGUI:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import domain.Login;
import domain.staff;
public class CreateGUI extends JFrame {
private ArrayList<staff> st = new ArrayList<staff>();
public CreateGUI() {
this.setLayout(new GridLayout(4, 2));
JTextField Text1;
JTextField Text2;
JLabel Label1 = new JLabel("Intranet Room Booking System");
JLabel Label2 = new JLabel("Staff ID:");
Text1 = new JTextField("");
JLabel Label3 = new JLabel("Staff PW:");
Text2 = new JTextField("");
JButton Button1 = new JButton("Login");
this.add(Label1);
this.add(new JLabel(""));
this.add(Label2);
this.add(Text1);
this.add(Label3);
this.add(Text2);
this.add(new JLabel(""));
this.add(Button1);
Button1.setBackground(Color.GREEN);
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a = Text1.getText();
String b = Text2.getText();
if (a.isEmpty() == true || b.isEmpty() == true) {
JOptionPane.showMessageDialog(null,"Invalid entry!");
} else {
if (Login.validate(a, b)) JOptionPane.showMessageDialog(null,"Right!");
else JOptionPane.showMessageDialog(null,"Wrong!");;
}
}
});
this.setLocation(100, 100);
this.setSize(new Dimension(900, 250));
this.setVisible(true);
}
}
我发现了一些问题:
无法读取类登录中的ArrayList;
在登录类中,a不能等于m;
我不知道如何解决错误......谢谢!
答案 0 :(得分:0)
你的错误
if (a == m) and (b==n)
return true;
应该是
if (a.equals(m)&&b.equals(n))
return true;
你也应该打电话
aa = new staff(c,d);
aa.theStaff(); // Nedd to call this to fill up the arraylist !
ArrayList<staff> st = aa.getst();
答案 1 :(得分:0)
==测试参考相等。
.equals()测试值的相等性。
因此,如果你真的想测试两个字符串是否具有相同的值,你应该使用.equals()(除了在一些情况下,你可以保证具有相同值的两个字符串将由同一个对象表示,例如:String interning)。
==用于测试两个字符串是否是同一个对象。
// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// concatenation of string literals happens at compile time,
// also resulting in the same object
"test" == "te" + "st" // --> true
// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) // --> false
// interned strings can also be recalled by calling .intern()
"test" == "!test".substring(1).intern() // --> true
重要的是要注意,==比equals()(单个引用比较而不是方法调用)便宜一点,因此,在适用的情况下(即,您可以保证您只处理它可以带来重要的性能提升。但是,这些情况很少见。
答案 2 :(得分:0)
每次创建新的员工时,我都不认为您需要新的员工列表
在staff
static
public class staff {
//blabla
private static ArrayList<staff> st =new ArrayList<staff>();
public staff (String StaffID, String StaffPW) {
//st = new ArrayList<staff>();
this.StaffID = StaffID;
this.StaffPW = StaffPW;
//eventually:
st.add(this);
}
public static void thestaff(){
//blabla
}
//blabla
public static ArrayList<staff> getst() {
return st;
}
}
这样,您可以使用staff.gestst()
要检查您的登录信息,请使用a.equals(m)
代替a==m
。
不要在String上使用==
。
顺便说一下,你应该用一个大写字母来编写你的类名。