- TS TS
- #621
This question has been answered
Question
5967994-518813.jpeg
Image transcriptions
2. Store 20 employee ID numbers in an integer array and 20 corresponding employee last names in a String array. Use dialog boxes to accept an ID number and display the appropriate last name.
This question was created from ArraysLab040309[1]
Answered by Expert Tutors
import javax.swing.JOptionPane;
public class NameSearch {
public static void main(String[] args) {
int idArray[]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
String lastNames[] = {"Mathew", "Hussy","Ponting","Trump","Obama","Palosi","Pence","Singh","Rabada","Polard",
"Simonds","Bewan","Clinton","Churchill","Kuls","Jhou","Benjamin","Franklin","Mike","Tison"};
String idInput = JOptionPane.showInputDialog("Enter Employee ID: ");
try{
int id = Integer.parseInt(idInput);
boolean isFound = false;
for(int i=0;i<idArray.length;i++)
{
if(idArray==id)
{
JOptionPane.showMessageDialog(null, "Last name of employee with ID "+id+" is : "+lastNames);
isFound = true;
}
}
if(!isFound)
{
JOptionPane.showMessageDialog(null, "Employee does not exist with ID "+id );
}
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Invalid ID input");
}
}
}
Step-by-step explanation
Please find the above java program.
It has two array to store the id and employee last name.
It will use the JOptionPane to take the user input and print the last name of the employee. IF id does not found then show the alert.
Please let me know if any issue.
The student who asked this found it Helpful
Overall rating 100%
"Thank you for answering my question. Godbless and I hope you get to teach more students like me ."
Question
5967994-518813.jpeg
Image transcriptions
2. Store 20 employee ID numbers in an integer array and 20 corresponding employee last names in a String array. Use dialog boxes to accept an ID number and display the appropriate last name.
This question was created from ArraysLab040309[1]
Answered by Expert Tutors
import javax.swing.JOptionPane;
public class NameSearch {
public static void main(String[] args) {
int idArray[]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
String lastNames[] = {"Mathew", "Hussy","Ponting","Trump","Obama","Palosi","Pence","Singh","Rabada","Polard",
"Simonds","Bewan","Clinton","Churchill","Kuls","Jhou","Benjamin","Franklin","Mike","Tison"};
String idInput = JOptionPane.showInputDialog("Enter Employee ID: ");
try{
int id = Integer.parseInt(idInput);
boolean isFound = false;
for(int i=0;i<idArray.length;i++)
{
if(idArray==id)
{
JOptionPane.showMessageDialog(null, "Last name of employee with ID "+id+" is : "+lastNames);
isFound = true;
}
}
if(!isFound)
{
JOptionPane.showMessageDialog(null, "Employee does not exist with ID "+id );
}
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Invalid ID input");
}
}
}
Step-by-step explanation
Please find the above java program.
It has two array to store the id and employee last name.
It will use the JOptionPane to take the user input and print the last name of the employee. IF id does not found then show the alert.
Please let me know if any issue.
The student who asked this found it Helpful
Overall rating 100%
"Thank you for answering my question. Godbless and I hope you get to teach more students like me ."
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.