12 Replies - 2239 Views - Last Post: 06 December 2011 - 10:47 PM Rate Topic: -----

#1 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Basic applet not functioning correctly, looping/array issue?

Posted 05 December 2011 - 11:05 PM

Okay, so I'm in an Intro to Java class, and we're currently covering applets. This assignment is giving me a hard time, it reads as follows:

"Create a JApplet that contains two parallel arrays that contain at least five employees' names and jobs. Allow the user to enter either a title or a name and to click a JButton to display the other. Include a JLabel to describe each JTextField."

I know the formatting of the JFrame is a bit awkward, but I'll fix that later, my main problem is that when someone enters something in the job title box and presses submit, it should give the name of the person with that job title, and vice versa. However, no matter what valid job title or name I enter into either JTextField, it always ends up reading "The Designer position is held by Susan" along with not removing the components that I want removed until it is minimized and brought back up. So it displays the wrong message, along with the removal problem.

Here is my html code:

<html>
<object  code = ”JEmployeeTitle2.class” width = 500 height = 500>
</object>
</html>




And here is my Java code:

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

public class JEmployeeTitle2 extends JApplet implements ActionListener
{
	JLabel jobLabel = new JLabel("Enter a job title below.");
	JLabel employeeLabel = new JLabel("Enter an employee name below.");
	Font font1 = new Font("Times New Roman", Font.BOLD, 20);
	JTextField jobTextField = new JTextField(20);
	JTextField employeeTextField = new JTextField(20);
	JButton submit = new JButton("Submit");
	JLabel lastLabel = new JLabel(" ");
	Container con = getContentPane();
	public void init()
	{
		jobLabel.setFont(font1);
		employeeLabel.setFont(font1);
		con.add(jobLabel);
		con.add(employeeLabel);
		con.add(jobTextField);
		con.add(employeeTextField);
		con.add(submit);
		con.setLayout(new FlowLayout());
		submit.addActionListener(this);
		jobTextField.addActionListener(this);
		employeeTextField.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
		String job = jobTextField.getText();
		String name = employeeTextField.getText();
		for(int x = 0; x < jobTitle.length; ++x)
		{
			if(job == jobTitle[x])
			{
				name = employeeName[x];
				con.remove(jobLabel);
				con.remove(employeeLabel);
				con.remove(jobTextField);
				con.remove(employeeTextField);
				con.remove(submit);
				lastLabel.setText("The " + jobTitle[x] + " position is held by " + name);
				lastLabel.setFont(font1);
				con.add(lastLabel);
			}
			else
			{
				for(int y = 0; y < employeeName.length; ++y)
				{
					if(name == employeeName[y]);
					{
						job = jobTitle[y];
						con.remove(jobLabel);
						con.remove(employeeLabel);
						con.remove(jobTextField);
						con.remove(employeeTextField);
						con.remove(submit);
						invalidate();
						validate();
						lastLabel.setText("name " + "holds the position of " + job);
						con.add(lastLabel);
					}
				}
			}
			invalidate();
			validate();
		}
	}
}



All help here is MUCH appreciated! Trying hard to figure this one out. I'll attach a screenshot of the box I end up with for a better idea.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Basic applet not functioning correctly, looping/array issue?

#2 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 05 December 2011 - 11:46 PM

name = employeeName[x];
job = jobTitle[y];



Now given you are looping through the arrays. What do you think name and job will end up as? It will always end up as the last index of the array, hence susan and designer.
Was This Post Helpful? 1
  • +
  • -

#3 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 12:26 AM

You're totally right, thanks a bunch for the response!

Trying to work out a loop/array system that will produce my desired outcome, if anybody has any input, that would be wonderful.
Was This Post Helpful? 0
  • +
  • -

#4 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 12:36 AM

Posting some updated code that I think may be getting at least a little closer, this one doesn't do anything when I press the "Submit" button though. Thinking that the problem is something with the Boolean variables. Help is MUCH appreciated.

public class JEmployeeTitle2 extends JApplet implements ActionListener
{
	boolean valid = false;
	JLabel jobLabel = new JLabel("Enter a job title below.");
	JLabel employeeLabel = new JLabel("Enter an employee name below.");
	Font font1 = new Font("Times New Roman", Font.BOLD, 20);
	JTextField jobTextField = new JTextField(20);
	JTextField employeeTextField = new JTextField(20);
	JButton submit = new JButton("Submit");
	JLabel lastLabel = new JLabel(" ");
	Container con = getContentPane();
	public void init()
	{
		jobLabel.setFont(font1);
		employeeLabel.setFont(font1);
		con.add(jobLabel);
		con.add(employeeLabel);
		con.add(jobTextField);
		con.add(employeeTextField);
		con.add(submit);
		con.setLayout(new FlowLayout());
		submit.addActionListener(this);
		jobTextField.addActionListener(this);
		employeeTextField.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
		String job = jobTextField.getText();
		String name = employeeTextField.getText();
		for(int x = 0; valid = false; ++x)
		{
			if(job == jobTitle[x])
			{
				valid = true;
				name = employeeName[x];
				con.remove(jobLabel);
				con.remove(employeeLabel);
				con.remove(jobTextField);
				con.remove(employeeTextField);
				con.remove(submit);
				invalidate();
				validate();
				lastLabel.setText("The " + jobTitle[x] + " position is held by " + name);
				lastLabel.setFont(font1);
				con.add(lastLabel);
			}
			else
			{
				if(name == employeeName[x]);
				{
						valid = true;
						job = jobTitle[x];
						con.remove(jobLabel);
						con.remove(employeeLabel);
						con.remove(jobTextField);
						con.remove(employeeTextField);
						con.remove(submit);
						invalidate();
						validate();
						lastLabel.setText("name " + "holds the position of " + job);
						lastLabel.setFont(font1);
						con.add(lastLabel);
				}
			}
			invalidate();
			validate();
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 12:55 AM

Something like this is much simpler. I didn't make a GUI though ( I hate them =P) but it shouldn't be hard to apply.

		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};

		int commonArrayLength = 5;
		boolean nameFound = false;

		Scanner getInput = new Scanner(System.in); // Replace this with GUI components to get input
		
		while (nameFound == false) {
			System.out.println("Please enter a valid title: ");
			String input = getInput.nextLine();
			for (int x = 0; x < commonArrayLength; ++x) {
				if (input.equals(jobTitle[x])) {
					System.out.println(jobTitle[x] + " is owned by " + employeeName[x]);
					nameFound = true;
				}
				if (input.equals(employeeName[x])) {
					System.out.println(employeeName[x] + " is a " + jobTitle[x]);
					nameFound = true;
				}
			}
		}
// Remove components
}



as for your code: for(int x = 0; valid = false; ++x) should have valid == false

This post has been edited by Mylo: 06 December 2011 - 12:55 AM

Was This Post Helpful? 1
  • +
  • -

#6 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 04:25 PM

Thanks a bunch man! That helps a lot!

Alright, so here is yet ANOTHER piece of code that I can't figure out why it won't work

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

public class JEmployeeTitle2 extends JApplet implements ActionListener
{
	JLabel jobLabel = new JLabel("Enter a job title below.");
	JLabel employeeLabel = new JLabel("Enter an employee name below.");
	Font font1 = new Font("Times New Roman", Font.BOLD, 20);
	JTextField jobTextField = new JTextField(20);
	JTextField employeeTextField = new JTextField(20);
	JButton submit = new JButton("Submit");
	JLabel lastLabel = new JLabel(" ");
	Container con = getContentPane();
	public void init()
	{
		jobLabel.setFont(font1);
		employeeLabel.setFont(font1);
		con.add(jobLabel);
		con.add(employeeLabel);
		con.add(jobTextField);
		con.add(employeeTextField);
		con.add(submit);
		con.setLayout(new FlowLayout());
		submit.addActionListener(this);
		jobTextField.addActionListener(this);
		employeeTextField.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		boolean valid = false;
		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
		String job = jobTextField.getText();
		String name = employeeTextField.getText();
		int x = 0;
		while(valid == false)
		{
			if(job == jobTitle[x])
			{
				valid = true;
				name = employeeName[x];
				con.remove(jobLabel);
				con.remove(employeeLabel);
				con.remove(jobTextField);
				con.remove(employeeTextField);
				con.remove(submit);
				lastLabel.setText("The " + jobTitle[x] + " position is held by " + name);
				lastLabel.setFont(font1);
				con.add(lastLabel);
			}
			else
			{
				if(name == employeeName[x]);
					{
						valid = true;
						job = jobTitle[x];
						con.remove(jobLabel);
						con.remove(employeeLabel);
						con.remove(jobTextField);
						con.remove(employeeTextField);
						con.remove(submit);
						invalidate();
						validate();
						lastLabel.setText("name " + "holds the position of " + job);
						con.add(lastLabel);
					}
			}
			++x;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 05:20 PM

Updating again, having a really tough time with this, if anybody sees absolutely anything wrong I would REALLY appreciate the help!

This one is back to always displaying "Susan holds the position of Designer" and I don't know why.

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

public class JEmployeeTitle2 extends JApplet implements ActionListener
{
	JLabel jobLabel = new JLabel("Enter a job title below.");
	JLabel employeeLabel = new JLabel("Enter an employee name below.");
	Font font1 = new Font("Times New Roman", Font.BOLD, 20);
	JTextField jobTextField = new JTextField(20);
	JTextField employeeTextField = new JTextField(20);
	JButton submit = new JButton("Submit");
	JLabel lastLabel = new JLabel(" ");
	Container con = getContentPane();
	public void init()
	{
		jobLabel.setFont(font1);
		employeeLabel.setFont(font1);
		con.add(jobLabel);
		con.add(employeeLabel);
		con.add(jobTextField);
		con.add(employeeTextField);
		con.add(submit);
		con.setLayout(new FlowLayout());
		submit.addActionListener(this);
		jobTextField.addActionListener(this);
		employeeTextField.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		boolean valid = false;
		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
		while(valid == false)
		{
			for(int x = 0; x < jobTitle.length; x++)
			{
				if(jobTextField.getText() == jobTitle[x])
				{
					valid = true;
					con.remove(jobLabel);
					con.remove(employeeLabel);
					con.remove(jobTextField);
					con.remove(employeeTextField);
					con.remove(submit);
					lastLabel.setText("The " + jobTitle[x] + " position is held by " + employeeName[x]);
					lastLabel.setFont(font1);
					con.add(lastLabel);
				}
				if(employeeTextField.getText() == employeeName[x]);
				{
					valid = true;
					con.remove(jobLabel);
					con.remove(employeeLabel);
					con.remove(jobTextField);
					con.remove(employeeTextField);
					con.remove(submit);
					lastLabel.setText(employeeName[x] + " holds the position of " + jobTitle[x]);
					con.add(lastLabel);
				}
			}
		}
		invalidate();
		validate();
	}
}



Was This Post Helpful? 0
  • +
  • -

#8 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 05:54 PM

You should use the .equals() method to compare strings.

Also, the while loop is not a good idea (didn't mean to mislead you sorry). It can be used, as long as the user can change the value in the box each iteration. Remove that for now since clicking the sumbit button is your form of iteration. You can also remove the valid boolean now.

This post has been edited by Mylo: 06 December 2011 - 05:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 07:04 PM

Can't thank you enough for all your help.

I've changed the code to the following, and its still outputting "The Designer position is held by Susan" no matter what I enter to either JTextField...I think the problem is that x is always going to end up equal to 4, and I really have no clue how to change that.

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

public class JEmployeeTitle2 extends JApplet implements ActionListener
{
	JLabel jobLabel = new JLabel("Enter a job title below.");
	JLabel employeeLabel = new JLabel("Enter an employee name below.");
	Font font1 = new Font("Times New Roman", Font.BOLD, 20);
	JTextField jobTextField = new JTextField(20);
	JTextField employeeTextField = new JTextField(20);
	JButton submit = new JButton("Submit");
	JLabel lastLabel = new JLabel(" ");
	Container con = getContentPane();
	public void init()
	{
		jobLabel.setFont(font1);
		employeeLabel.setFont(font1);
		con.add(jobLabel);
		con.add(employeeLabel);
		con.add(jobTextField);
		con.add(employeeTextField);
		con.add(submit);
		con.setLayout(new FlowLayout());
		submit.addActionListener(this);
		jobTextField.addActionListener(this);
		employeeTextField.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
		String job = jobTextField.getText();
		String name = employeeTextField.getText();
		for(int x = 0; x < 5; x++)
		{
			if(job.equals(jobTitle[x]))
			{
				con.remove(jobLabel);
				con.remove(employeeLabel);
				con.remove(jobTextField);
				con.remove(employeeTextField);
				con.remove(submit);
				lastLabel.setText("The " + jobTitle[x] + " position is held by " + employeeName[x]);
				lastLabel.setFont(font1);
				con.add(lastLabel);
			}
			if(name.equals(employeeName[x]));
			{
				con.remove(jobLabel);
				con.remove(employeeLabel);
				con.remove(jobTextField);
				con.remove(employeeTextField);
				con.remove(submit);
				lastLabel.setText(employeeName[x] + " holds the position of " + jobTitle[x]);
				con.add(lastLabel);
			}
		}
		invalidate();
		validate();
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 08:07 PM

Ah, forgot to mention

if(name.equals(employeeName[x]));



the semicolon makes the if statement useless, so it will always go to the end. Just remove the semicolon
Was This Post Helpful? 1
  • +
  • -

#11 AlexAndAHalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 08:10 PM

Haha perfect! Its functioning exactly as desired, thanks so much for all your help man, hopefully I can pull an A in this class now!
Was This Post Helpful? 0
  • +
  • -

#12 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 08:20 PM

Well to further improve, you can move the

String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};

outside the method, as it's always going to remain the same.

You could also have two buttons, one that checks for names and one that checks for jobTitles instead of throwing them together in the same button. Or atleast don't allow the user to enter values in both boxes.

Maybe add some messages if a username/jobtitle isn't found

And ofcourse... use a layout manager =P

This post has been edited by Mylo: 06 December 2011 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Basic applet not functioning correctly, looping/array issue?

Posted 06 December 2011 - 10:47 PM

Better to declare your arrays as instance variables rather than local variables to your actionPerformed() method. That way, they are not re-created each time the actionPerformed() method is invoked. Also, don't remove JComponents from your GUI. Just get/set their text as necessary. Or toggle their visibility as appropriate.

Lastly, a more standards adherent practice is to use classes and objects over parallel arrays. My tutorial Moving Away From Parallel Arrays covers this in more detail.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1