I'm trying to write an external log-in class for my Adv Java class in school. It's an online section and the teacher is very hard to reach thus why I'm asking for help here :). But my problem is right now I have my log-in form and I have it accepting (not validating yet just accepting) a user name passing it to my external class adding it to my array (because later I need to be able to delete it from the array to "log-off". This is what I have so far for my external class:

import java.util.*;

public class Login
{
	private ArrayList loginHistory;
	
	public Login(String newLogin) throws Exception
	{
		
		loginHistory = new ArrayList();
		set(newLogin);
	}
	
	public void set(String login) throws Exception
	{
		boolean loginAdded = true;
		
		login = login.trim();
		
		loginAdded = loginHistory.add(login);
	}
}//end of Login class

and this is what I have for my application demo part

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginDemo
{
	public static void main(String[] argv)
	{
		int width = 210;
		int height = 140;
		final demoFrame f = new demoFrame("LoginDemo");
		
		f.pack();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(width, height);
		f.centerOnScreen(width, height);
		f.setVisible(true);
	}
}//end class LoginDemo

class demoFrame extends JFrame implements ActionListener
{
	Login login = null;
	String userName;
	Integer counter = 0;
	
	JTextField userNameField;
	JTextField userCountField;
	JButton jbtLogOn, jbtLogOff;
	
	public demoFrame(String title)
	{
		super(title);
		
		JLabel label1 = new JLabel("User: ");
		userNameField = new JTextField(10);
		JLabel label2 = new JLabel("Number of Users: ");
		userCountField = new JTextField(5);
		userCountField.setEditable(false);
		
		jbtLogOn = new JButton("Logon");
		jbtLogOff = new JButton("Logoff");
		jbtLogOn.addActionListener(this);
		jbtLogOff.addActionListener(this);
		
		JPanel userPanel = new JPanel(new BorderLayout (10,10));
		userPanel.add(label1, BorderLayout.WEST);
		userPanel.add(userNameField, BorderLayout.EAST);
		
		JPanel buttonPanel = new JPanel(new FlowLayout());
		buttonPanel.add(jbtLogOn);
		buttonPanel.add(jbtLogOff);
		
		JPanel userCountPanel = new JPanel(new BorderLayout(10,10));
		userCountPanel.add(label2, BorderLayout.WEST);
		userCountPanel.add(userCountField, BorderLayout.EAST);
		
		JPanel contentPanel = new JPanel(new FlowLayout());
		contentPanel.add(userPanel);
		contentPanel.add(buttonPanel);
		contentPanel.add(userCountPanel);
		setContentPane(contentPanel);
		
		InputMap map;
		map = jbtLogOn.getInputMap();
		if (map != null)
		{
			map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false), "pressed");
			map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true), "release");
		}
		map = jbtLogOff.getInputMap();
		if (map != null)
		{
			map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false), "pressed");
			map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true), "release");
		}
	}
	
	public void centerOnScreen(int width, int height)
	{
		int top, left, x, y;
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		x = (screenSize.width - width)/2;
		y = (screenSize.height - height)/2;
		top = (x < 0) ? 0 : x;
		left = (y < 0) ? 0 : y;
		
		this.setLocation(top, left);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		try
		{
			if(e.getSource() == jbtLogOn)
			{
				userName = new String(userNameField.getText());
				login = new Login(userName);
				userNameField.setText("");
				userNameField.requestFocus();
				counter++;
				userCountField.setText(counter.toString());
			}
			
			if(e.getSource() == jbtLogOff)
			{
				userNameField.setText("");
				userNameField.requestFocus();
				if(counter != 0)
				{
					counter--;
					userCountField.setText(counter.toString());
				}
				else
				{
					JOptionPane.showMessageDialog(this, "No username yet logged in", "Invalid userName. Try Again.", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		catch (NullPointerException ex)
		{
			JOptionPane.showMessageDialog(this, "No username yet logged in", "Invalid userName. Try Again.", JOptionPane.ERROR_MESSAGE);
		}
		catch (Exception ex)
		{
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Invalid username. Try again.", JOptionPane.ERROR_MESSAGE);
		}	
	}
}//end class demoFrame

my problem is that every time I hit add it starts a new blank arrayList for my loginHistory which i see that it's doing it on line 10 in my external class, I just don't know how to change it functionally so that it doesn't do that and I can add multiple names through the list. Also if somebody could help me instead of having just a counter for my second label box ("Number of Users Logged in") (line 103 in LoginDemo), is there a way to make that text box read the amount that is in the arrayList so that it's always accurate in the count of how many is logged in. Thank you to anybody that can help me.

In your Login class, you have a method called set. It should be called "add" because it adds a String to the ArrayList. And I don't see what line you're talking about. What line number is it on (as far as the line numbers to the left of your code that you posted in here)? Because it doesn't seem to be on line ten.

when I look at it, it shows line 10 on the code that is posted but it's the line "loginHistory = new ArrayList();" i know that this line is running every time I hit my add button but I don't want it to lol I want just one ArrayList per time the program is loaded if that makes sense.

Ok, I see what you mean now. So on line 100, put

if (login == null) login = new Login(userName);

Putting that will have the effect of only creating a new Login if you haven't already created one before.

Like I said, you should change your "set" method to "add". You should also have another method called "remove" that takes a String (the username) and attempts to remove it from the ArrayList. So any time the user logs on, they should be added to the ArrayList (meaning you should call the add method) and any time the user logs off they should be removed from the ArrayList. Keep in mind that it is possible for the user to attempt to log in more than once - your code should ignore the user's login attempts if they are already logged in. The ArrayList class has a contains method that will tell you (by returning true) if your User is already in the ArrayList.

thank you very much that worked perfectly. Now do you know how to do the second part of my question about the counter tracker. Instead of keeping just a "counter++" is there a way to say something like:
"userNameField = loginHistory.Length" ?

Thank you very much BestJewSinceJC I just was able to finish my lab with your help, and I understand what I was doing wrong.

No problem, glad I could help!

I do have one more question actually, I have everything working except the catching of an empty string when the user doesn't put in a userName it runs through and before I add it to the arrayList I have this line

if(login == "")
		{
			throw new Exception("User must first enter a UserID");
		}

and even though right as this is executed my debuggers say login = "" i don't know why it's not catching.

That's because you cannot use "==" except on primitive types. That means you can only use "==" on int, double, float, etc. For Objects, if you use "==" it is just checking to see if the memory address of "login" is the same as the memory address of "" (which is the empty String). Of course, these memory addresses are not the same, so it will never work. Anyway, the correct way to do it is this:

if (login.equals("")){
//do something in here
}

Thank you again, you've been a lot of help. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.