My assignment is as follows: Create aJApplet that contains twoJTextFields, aJButton, and threeJLabels. When the user types an employee’ s first and last names (separated by a space) in a JTextField, the employee ’ s job title is displayed in a secondJTextField. Include two JLabels to describe theJTextFields used for data entry, and include a third JLabel that holds the employee ’ s title or an error message if no match is found for the employee. Use parallel arrays to store the employees ’ names and job titles. Save the file as JEmployeeTitle.java

I have having an issue specifically with the output of this code, everything compiles correctly, and the only issue I seem to be having is with the output of the name and title in line 81, I believe it is. Any help is appreciated.

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

public class JEmployeeTitle extends JApplet implements ActionListener 
{
    //initialize arrays
    String[] Names = {"Maxwell Rudolph", "David Morand", "Andrew Turner", "Andrew Lacquiere", "Joseph Go",
        "Ron Kallam", "Tracy Kallam", "Kaylee Morand", "Cindy Morand", "Tim Morand"};
    String[] Title = {"Customer Solutions Supervisor", "Customer Service Associate", "Customer Service Associate", 
        "Technical Support Associate", "Technical Support Associate", "QA Supervisor", "VP of Operations", "Daughter", 
        "Teacher", "Program Analyst"};
    
    //create labels
    JLabel fullNameLabel = new JLabel("Please enter your first and last name separated by a space");
    JLabel nameDescription = new JLabel("Your Name");
    JLabel jobTitleLabel = new JLabel("");
    
    //set font
    Font mainFont = new Font("Times New Roman", Font.PLAIN, 14);
    
    //create text fields
    JTextField fullNameText = new JTextField(25);
    JTextField jobTitleText = new JTextField(15);
    
    //create button
    JButton findTitle = new JButton("Find the title");
    
    //set container
    Container con = getContentPane();

    @Override
    public void init() 
    {
        //set parameters for pulling from the array
        final int numberOfItems = 10;
        String arrayNum = "";
        boolean validItem;
        
        //for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[x])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[x]);
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }
        
        //set fonts for labels
        fullNameLabel.setFont(mainFont);
        nameDescription.setFont(mainFont);
        jobTitleLabel.setFont(mainFont);
        
        //add labels to the container
        con.add(fullNameLabel);
        con.add(fullNameText);
        con.add(jobTitleText);
        con.add(findTitle);
        
        //set the layout for the container
        con.setLayout(new FlowLayout());
        
        //event handling
        findTitle.addActionListener(this);
        fullNameText.addActionListener(this);
        jobTitleText.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        //set action to be taken upon button click
        String name = fullNameText.getText();
        
        //set the text for the job title label
        jobTitleText.setText("The job title of " + Names + "is " + Title);
        
        //add the label for the job title
        con.add(jobTitleLabel);
        validate();
    }
}

The output I am receiving looks like this: The job title of [Ljava.lang.String;@defa1ais [Ljava.lang.String;@f5da06

The values you are seeing:[Ljava.lang.String;@defa1ais [Ljava.lang.String;@f5da06 are the Strings returned by the default toString method for String arrays. If you want to see the full contents of the arrays, you could use the Arrays class's toString() method to format it.

If you only want to see one element from the array, you need to use an index (enclosed in [])

My assignment is as follows: Create aJApplet that contains twoJTextFields, aJButton, and threeJLabels. When the user types an employee’ s first and last names (separated by a space) in a JTextField, the employee ’ s job title is displayed in a secondJTextField. Include two JLabels to describe theJTextFields used for data entry, and include a third JLabel that holds the employee ’ s title or an error message if no match is found for the employee. Use parallel arrays to store the employees ’ names and job titles. Save the file as JEmployeeTitle.java

I have having an issue specifically with the output of this code, everything compiles correctly, and the only issue I seem to be having is with the output of the name and title in line 81, I believe it is. Any help is appreciated.

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

public class JEmployeeTitle extends JApplet implements ActionListener 
{
    //initialize arrays
    String[] Names = {"Maxwell Rudolph", "David Morand", "Andrew Turner", "Andrew Lacquiere", "Joseph Go",
        "Ron Kallam", "Tracy Kallam", "Kaylee Morand", "Cindy Morand", "Tim Morand"};
    String[] Title = {"Customer Solutions Supervisor", "Customer Service Associate", "Customer Service Associate", 
        "Technical Support Associate", "Technical Support Associate", "QA Supervisor", "VP of Operations", "Daughter", 
        "Teacher", "Program Analyst"};
    
    //create labels
    JLabel fullNameLabel = new JLabel("Please enter your first and last name separated by a space");
    JLabel nameDescription = new JLabel("Your Name");
    JLabel jobTitleLabel = new JLabel("");
    
    //set font
    Font mainFont = new Font("Times New Roman", Font.PLAIN, 14);
    
    //create text fields
    JTextField fullNameText = new JTextField(25);
    JTextField jobTitleText = new JTextField(15);
    
    //create button
    JButton findTitle = new JButton("Find the title");
    
    //set container
    Container con = getContentPane();

    @Override
    public void init() 
    {
        //set parameters for pulling from the array
        final int numberOfItems = 10;
        String arrayNum = "";
        boolean validItem;
        
        //for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[x])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[x]);
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }
        
        //set fonts for labels
        fullNameLabel.setFont(mainFont);
        nameDescription.setFont(mainFont);
        jobTitleLabel.setFont(mainFont);
        
        //add labels to the container
        con.add(fullNameLabel);
        con.add(fullNameText);
        con.add(jobTitleText);
        con.add(findTitle);
        
        //set the layout for the container
        con.setLayout(new FlowLayout());
        
        //event handling
        findTitle.addActionListener(this);
        fullNameText.addActionListener(this);
        jobTitleText.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        //set action to be taken upon button click
        String name = fullNameText.getText();
        
        //set the text for the job title label
        jobTitleText.setText("The job title of " + Names + "is " + Title);
        
        //add the label for the job title
        con.add(jobTitleLabel);
        validate();
    }
}

The output I am receiving looks like this: The job title of [Ljava.lang.String;@defa1ais [Ljava.lang.String;@f5da06

adding to what NormR1 said, make a global int variable that has -1 as its starting value, once they enter their name/job title search through the relevant array until you find the matching name/job title and then save this number to the global int variable, now when you print the name and title use Names[] and Title[] and inside the '[]' will be the global int you declared which has the index of the name -which if you are using flat/synchronized arrays-would be the same in both cases. by this i mean if the name 'Maxwell Rudolph' was entered the corresponding title in a flat/synchronized array would be 'Customer Solutions Supervisor'.

Edited 4 Years Ago by DavidKroukamp: n/a

The values you are seeing:[Ljava.lang.String;@defa1ais [Ljava.lang.String;@f5da06 are the Strings returned by the default toString method for String arrays. If you want to see the full contents of the arrays, you could use the Arrays class's toString() method to format it.

If you only want to see one element from the array, you need to use an index (enclosed in [])

So on line 81:

jobTitleText.setText("The job title of " + Names.toString() + "is " + Title.toString());

If I want it to display the entire array. How do I write it with an index, when I tried:

jobTitleText.setText("The job title of " + Names[x] + "is " + Title[x]);

I received syntax errors.

//Author Dave Morand
//Description Assignment for CIS263AA
//Date 14 January 2012
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JEmployeeTitle extends JApplet implements ActionListener 
{
    //initialize arrays
    String[] Names = {"Maxwell Rudolph", "David Morand", "Andrew Turner", "Andrew Lacquiere", "Joseph Go",
        "Ron Kallam", "Tracy Kallam", "Kaylee Morand", "Cindy Morand", "Tim Morand"};
    String[] Title = {"Customer Solutions Supervisor", "Customer Service Associate", "Customer Service Associate", 
        "Technical Support Associate", "Technical Support Associate", "QA Supervisor", "VP of Operations", "Daughter", 
        "Teacher", "Program Analyst"};
    
    //create labels
    JLabel fullNameLabel = new JLabel("Please enter your first and last name separated by a space");
    JLabel nameDescription = new JLabel("Your Name");
    JLabel jobTitleLabel = new JLabel("");
    
    //set font
    Font mainFont = new Font("Times New Roman", Font.PLAIN, 14);
    
    //create text fields
    JTextField fullNameText = new JTextField(25);
    JTextField jobTitleText = new JTextField(15);
    
    //create button
    JButton findTitle = new JButton("Find the title");
    
    //set container
    Container con = getContentPane();
    
    int specSelect = -1;

    @Override
    public void init() 
    {
        //set parameters for pulling from the array
        final int numberOfItems = 10;
        String arrayNum = "";
        boolean validItem;
        
        //for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[specSelect])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[specSelect]);
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }
        
        //set fonts for labels
        fullNameLabel.setFont(mainFont);
        nameDescription.setFont(mainFont);
        jobTitleLabel.setFont(mainFont);
        
        //add labels to the container
        con.add(fullNameLabel);
        con.add(fullNameText);
        con.add(jobTitleText);
        con.add(findTitle);
        
        //set the layout for the container
        con.setLayout(new FlowLayout());
        
        //event handling
        findTitle.addActionListener(this);
        fullNameText.addActionListener(this);
        jobTitleText.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        //set action to be taken upon button click
        String name = fullNameText.getText();
        
        //set the text for the job title label
        jobTitleLabel.setText("The job title of " + Names[specSelect] + "is " + Title[specSelect]);
        
        //add the label for the job title
        con.add(jobTitleLabel);
        validate();
    }
}

This is what I am doing now. But when I run the applet it says it is not initialized. I understand how code works still have a bit of trouble converting conversation into applicable code, if you can post an example I would appreciate it.

The error I am receiving from the NetBeans compiler is this:

run:
java.lang.ArrayIndexOutOfBoundsException: -1
	at JEmployeeTitle.init(JEmployeeTitle.java:48)
	at sun.applet.AppletPanel.run(AppletPanel.java:424)
	at java.lang.Thread.run(Thread.java:619)
BUILD SUCCESSFUL (total time: 6 seconds)

The error says that -1 is NOT a valid index for an array.
You need to select an index between 0 and the length of the array -1.

Which element in the arrays do you want to display?
Should there be separate indexes for the names array and for the title array?
One to chose the person's name, the other to chose his title.

The error I am receiving from the NetBeans compiler is this:

run:
java.lang.ArrayIndexOutOfBoundsException: -1
	at JEmployeeTitle.init(JEmployeeTitle.java:48)
	at sun.applet.AppletPanel.run(AppletPanel.java:424)
	at java.lang.Thread.run(Thread.java:619)
BUILD SUCCESSFUL (total time: 6 seconds)

look here:

//for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[specSelect])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[specSelect]);
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }

you should have this or similar:

//for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[x])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[x]);
                specSelect=x;
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }

elect is set to -1 and in the for loop you are declaring the integer x to be increased as the for loop iterates. so the Names and Titles will have to use the x value in order to iterate through all the possible values, the specSelect will be used to store the index value at which the name of the person and their job title was found... and then it will be used to display is later by:

jobTitleLabel.setText("The job title of " + Names[specSelect] + "is " + Title[specSelect]);

Edited 4 Years Ago by DavidKroukamp: n/a

The error says that -1 is NOT a valid index for an array.
You need to select an index between 0 and the length of the array -1.

Which element in the arrays do you want to display?
Should there be separate indexes for the names array and for the title array?
One to chose the person's name, the other to chose his title.

The Array needs to display only one name and the title from the parallel array.

So when someone enters the name (ie. David Morand) it displays the title from the title array of Customer Service Associate.

look here:

//for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[specSelect])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[specSelect]);
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }

you should have this or similar:

//for loop to run the array pull
        for (int x = 0; x < numberOfItems; ++x) 
        {
            if (arrayNum.equals(Names[x])) 
            {
                validItem = true;
                jobTitleLabel.setText(Title[x]);
                specSelect=x;
            }
            else
            {
                jobTitleLabel.setText("Person is not found.");
            }
        }

elect is set to -1 and in the for loop you are declaring the integer x to be increased as the for loop iterates. so the Names and Titles will have to use the x value in order to iterate through all the possible values, the specSelect will be used to store the index value at which the name of the person and their job title was found... and then it will be used to display is later by:

jobTitleLabel.setText("The job title of " + Names[specSelect] + "is " + Title[specSelect]);

I did this and it returned a whole new slew of errors:

run:
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: -1
	at JEmployeeTitle.actionPerformed(JEmployeeTitle.java:87)
	at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
	at javax.swing.JTextField.postActionEvent(JTextField.java:705)
	at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
	at javax.swing.JComponent.processKeyBinding(JComponent.java:2849)
	at javax.swing.JComponent.processKeyBindings(JComponent.java:2884)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2812)
	at java.awt.Component.processEvent(Component.java:5911)
	at java.awt.Container.processEvent(Container.java:2023)
	at java.awt.Component.dispatchEventImpl(Component.java:4501)
	at java.awt.Container.dispatchEventImpl(Container.java:2081)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
	at java.awt.Component.dispatchEventImpl(Component.java:4373)
	at java.awt.Container.dispatchEventImpl(Container.java:2081)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: -1
	at JEmployeeTitle.actionPerformed(JEmployeeTitle.java:87)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6134)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
	at java.awt.Component.processEvent(Component.java:5899)
	at java.awt.Container.processEvent(Container.java:2023)
	at java.awt.Component.dispatchEventImpl(Component.java:4501)
	at java.awt.Container.dispatchEventImpl(Container.java:2081)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
	at java.awt.Container.dispatchEventImpl(Container.java:2067)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 14 seconds)

You are still using an invalid array index value of -1.
Change that index variable to have a value of 0 and then you will not get this error.
However, you will then always show the first name and title from the arrays.

Edited 4 Years Ago by NormR1: n/a

specSelect should be only used to store the index at which the name/job title was found, and this would be 'x' in the for loop and not as you did Names[specSelect] and Titles[specSelect]...if you dont use 'x' you will never transverse between the indexes of the arrays, have a look at my previous post.

Edited 4 Years Ago by DavidKroukamp: n/a

specSelect should be only used to store the index at which the name/job title was found, and this would be 'x' in the for loop and not as you did Names[specSelect] and Titles[specSelect]...if you dont use 'x' you will never transverse between the indexes of the arrays, have a look at my previous post.

Right that is what I did, that is what brought the additional errors.

Also @Norm, I changed it to 0 and you are correct it comes up as only Maxwell Rudolph, this does not work for me. I need it to pull the title for whatever name has been entered.

pull the title for whatever name has been entered.

How would you do that manually? If the names and titles were listed on a piece of paper and you were given the name, how would you find the title?
When you find the row that the name is on, the title is on the same row in another column.
The columns on the paper are the same as the arrays in your program.
The row on the piece of paper is the same as the index into the arrays.

This article has been dead for over six months. Start a new discussion instead.