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

[9208 byte] By [codboya] at [2007-11-26 21:11:07]
# 1

Paste in the entire error message, and indicate clearly which line it's coming from.

An example of that error and what causes it: int foo () {}

...

String str = foo();

:; javac Z.java

Z.java:6: incompatible types

found: int

required: java.lang.String

String str = foo();

^

foo returns an int. str is not an int.

jverda at 2007-7-10 2:48:26 > top of Java-index,Java Essentials,Java Programming...
# 2
I'm using JCreator so the only error message I get is:Incompatible TypesIt's in the find method:String[] creds = users.get(UName);I guess you can't put a key from a hashmap into a string array. I just don't know how to get around this error.Thanks
codboya at 2007-7-10 2:48:26 > top of Java-index,Java Essentials,Java Programming...
# 3

First of all, this code is mine, from another thread:

FileInputStream fis = new FileInputStream("UP1.txt");

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

while ((UP1Line = br.readLine()) != null) {

...

}

And I want my 5 dukes!

Second, you probably just need to cast the return value from the get() method, which is an Object, to a String[]:String[] creds = (String[]) users.get(UName);

If you are using Java 1.5 or greater, consider using generics to avoid that sort of problem.

Geoff

glevnera at 2007-7-10 2:48:26 > top of Java-index,Java Essentials,Java Programming...
# 4

> I'm using JCreator so the only error message I get

> is:

>

> Incompatible Types

If that's really all it says, then get rid of JCreator now. However, I suspect there's more, but you're just not seeing it.

> String[] creds = users.get(UName);

>

> I guess you can't put a key from a hashmap into a

> string array. I just don't know how to get around

> this error.

I assume each key is a string. But what you're trying to do here is say "This array of strings is equal to this single string." Do you think it makes sense to do that?

jverda at 2007-7-10 2:48:26 > top of Java-index,Java Essentials,Java Programming...
# 5
Thank you so much, that did the trick. How do I give you your dukes? Please message back, you deserve them. And Thanks again. I'd thought I'd give you 10, you deserve them.ColinMessage was edited by: codboy
codboya at 2007-7-10 2:48:26 > top of Java-index,Java Essentials,Java Programming...