Q:

Swing Java Employee Program

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
twoJLabels 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
Every thing works fine but when i include following part of code only error message "Employee not found" even if array matches.Without this statement code works fine.please help..thanks
 if(!(name.equalsIgnoreCase(empName[x]) ||    job.equalsIgnoreCase(jobName[x])))
    { 
        errorortitle.setText("Employee not found");

    }  

 
 package appletLesson;

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

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*;
import java.awt.Color.*; 
public class JEmployeeTitle extends JApplet implements ActionListener 
{ 
String[] empName = {"James", "Tom", "Steve", "Barack", "John"}; 
String[] jobName = {"Blunt", "Kites", "Jobs", "Obama", "Smith"}; 
String[] jobTitle = {"Actor", "Buisness men", "CEO", "President", "Painter"}; 

JLabel fnameLabel = new JLabel("Enter First Name:"); 
JLabel lnameLabel = new JLabel("ENter Last Name:");
JButton button = new JButton("Submit"); 
JLabel errorortitle = new JLabel(" "); 
JTextField fnameField = new JTextField(20); 
JTextField lnameField = new JTextField(20); 

Container con = getContentPane(); 
public void init() 
{ 
    con.setBackground(Color.YELLOW);    

    con.add(fnameLabel); 

    con.add(fnameField);
    con.add(lnameLabel);
    con.add(lnameField); 
    con.add(button); 
    con.add(errorortitle);
    con.setLayout(new FlowLayout()); 
    fnameField.addActionListener(this); 
    lnameField.addActionListener(this);
    button.addActionListener(this); 
} 
public void actionPerformed(ActionEvent e) 
{ 
    String name = fnameField.getText();
    String job = lnameField.getText(); 
    for(int x = 0; x < 5; x++) 
    { 
        if(name.equalsIgnoreCase(empName[x]) ||           job.equalsIgnoreCase(jobName[x]))
        { 
            errorortitle.setText(jobTitle[x]);

        } 
        if(!(name.equalsIgnoreCase(empName[x]) ||    job.equalsIgnoreCase(jobName[x])))
        { 
            errorortitle.setText("Employee not found");

        } 
            }
    validate(); 
} 
}

A:

Your problem is in this loop:
for(int x = 0; x < 5; x++) 
{ 
    if(name.equalsIgnoreCase(empName[x]) || job.equalsIgnoreCase(jobName[x]))
    { 
        errorortitle.setText(jobTitle[x]);

    } 
    if(!(name.equalsIgnoreCase(empName[x]) || job.equalsIgnoreCase(jobName[x])))
    { 
        errorortitle.setText("Employee not found");

    } 
}

You should break out of the loop once you find a match, otherwise you'll just continue through all of the names and overwrite the correct jobTitle with "Employee not found". (I bet your original code works correctly with John Smith and nobody else -- try it.) So, e.g.:
for(int x = 0; x < 5; x++) 
{ 
    if(name.equalsIgnoreCase(empName[x]) ||           job.equalsIgnoreCase(jobName[x]))
    { 
        errorortitle.setText(jobTitle[x]);
        break; // stop checking
    } 
    if(!(name.equalsIgnoreCase(empName[x]) ||    job.equalsIgnoreCase(jobName[x])))
    { 
        errorortitle.setText("Employee not found");

    } 
}

Also I wonder if you mean && instead of ||, but I don't know exactly what you're going for.
In situations like this it helps to just sit down and look at your code closely. Step through it in your head one line at a time (or use a debugger), and see if you can spot the problem. Write out iterations on paper if it helps.
Hope that helps.
By the way, yes, there are cleaner ways to write the above loops. I'll leave that as an exercise to the OP.

