Searching Hashmaps
From my previous posts, I've started up programming again. Some of the concepts are old, but most are new. For example, hashmaps. Never used them before. I somwhat understand the documentation nut not completely. I'm trying to program a login method. Here's part of my code:
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
publicclass EmailFormsextends JFrameimplements ActionListener{
private JTextField jtfUserName;
private JPasswordField jpfPassword;
private JButton jbtLogin;
private JButton jbtCancel;
private HashMap users;
publicstaticvoid main (String[] args){
EmailForms LoginFrame =new EmailForms();
LoginFrame.setSize(200, 100);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
Dimension frameSize = LoginFrame.getSize();
int x = (screenWidth - frameSize.width)/2;
int y = (screenHeight - frameSize.height)/2;
LoginFrame.setLocation(x, y);
LoginFrame.pack();
LoginFrame.setVisible(true);
}
public EmailForms (){
users =new HashMap();
try{
String UP1Line;
FileInputStream fis =new FileInputStream("UP1.txt");
BufferedReader br =new BufferedReader(new InputStreamReader(fis));
while ((UP1Line = br.readLine()) !=null){
String [] items = UP1Line.split(" ", 4);
users.put(items[0],new String[]{ items[1], items[2]});
if (items[2].matches("[Yy]")){
System.err.println("y");
}
else{
System.err.println("n");
}
}
br.close();
fis.close();
}
catch (IOException ex){
JOptionPane.showMessageDialog(this,"Fatal Systems Error","Email Forms: File Error 1",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
catch (NullPointerException ex){
JOptionPane.showMessageDialog(this,"Fatal Systems Error","Email Forms: File Error 2",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
setTitle("E-Forms: Login");
JPanel jpLoginLabels =new JPanel();
jpLoginLabels.setLayout(new GridLayout(2, 1));
jpLoginLabels.add(new JLabel("Username:"));
jpLoginLabels.add(new JLabel("Password:"));
JPanel jpLoginText =new JPanel();
jpLoginText.setLayout(new GridLayout(2, 1));
jpLoginText.add(jtfUserName =new JTextField(10));
jpLoginText.add(jpfPassword =new JPasswordField(10));
JPanel p1 =new JPanel();
p1.setLayout(new BorderLayout());
p1.add(jpLoginLabels, BorderLayout.WEST);
p1.add(jpLoginText, BorderLayout.CENTER);
JPanel p2 =new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
p2.add(jbtLogin =new JButton("Login"));
p2.add(jbtCancel =new JButton("Cancel"));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);
jbtLogin.addActionListener(this);
jbtCancel.addActionListener(this);
}
publicvoid actionPerformed(ActionEvent e){
AdminEForms AEFMain =new AdminEForms();
UserEForms UEFMain =new UserEForms();
String arg = e.getActionCommand();
int index = find(jtfUserName.getText().trim(),new String(jpfPassword.getPassword()));
if (arg =="Cancel"){
System.exit(0);
arg ="";
}
if (arg =="Login"){
if (index == -1){
JOptionPane.showMessageDialog(this,"Username/Password Not Found","Email Forms: Login Error",
JOptionPane.INFORMATION_MESSAGE);
}
else{
if (index == 1 ){
JOptionPane.showMessageDialog(this,"Username and Password Found: Admin","Email Forms: Good",
JOptionPane.INFORMATION_MESSAGE);
AEFMain.AFrameSetup();
}
else{
JOptionPane.showMessageDialog(this,"Username and Password Found: User","Email Forms: Good",
JOptionPane.INFORMATION_MESSAGE);
UEFMain.UFrameSetup();
}
}
}
arg ="";
}
publicint find(String UName, String PWord){
String[] creds = users.get(UName);
if (creds!=null && creds[0].matches(PWord)){
System.out.println("ok");
if (creds[1].matches("[Yy]"))
return 1;
else
return 0;
}
System.err.println("not ok");
return -1;
}
}
Since I have a limited understanding of hashmaps, I don't even know if it's being populated with the information from my file. But I'm getting this error in the find method:
Incompatible types
I've done some reading and I think I know why, but I don't know how to fix it. Please help.
Colin
Oh Yeah, my text file looks like this:
9876543 9876543 Y
0612207 0612207 N
0123456 0123456 N
1234567 1234567 Y
Thanks in advance

