0

I got some trouble in my app.

In my validate function, even when a and m are the same, and b and n too, it still return false.

Still in validate function, the ArrayList<staff> st = aa.getst(); call returns an empty list.

Staff:

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;
   }
}

'

Login

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;
}
}

'

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

I found some of the problems:

  1. the ArrayList in class login cannot be read;

  2. in class login, a cannot equal to m;

i don't know how to fix the mistakes...thank you!

Dummy00001
  • 16,630
  • 5
  • 41
  • 63
RileyTse
  • 1
  • 5
  • 2
    **Do not use == to compare Strings**. http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839#513839 – ajb Nov 13 '14 at 15:56

3 Answers3

0

Your if is wrong

if (a == m) and (b==n)
    return true;

Should be

if (a.equals(m)&&b.equals(n))
    return true;

Also you should be calling

aa = new staff(c,d);
aa.theStaff(); // Nedd to call this to fill up the arraylist !
ArrayList<staff> st = aa.getst();
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

== tests for reference equality.

.equals() tests for value equality.

Consequently, if you actually want to test whether two strings have the same value you should use .equals() (except in a few situations where you can guarantee that two strings with the same value will be represented by the same object eg: String interning).

== is for testing whether two strings are the same object.

// 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

It is important to note that == is a bit cheaper than equals() (a single reference comparison instead of a method call), thus, in situations where it is applicable (i.e. you can guarantee that you are only dealing with interned strings) it can present an important performance improvement. However, these situations are rare.

0

I don't think you want a new list of staff each time you create a new staff memeber so make your ArrayList in 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;
}
}

This way, you can acces your staff list using staff.gestst()

To check your login use a.equals(m) instead of a==m. Don't use == on String.

By the way, you should wrote your class names with a capital char.

jhamon
  • 3,603
  • 4
  • 26
  • 37