-2

Tried this, didn't work. The purpose of this program is to set employees.

Scanner input = new Scanner(System.in);
Employeeobj f1 = new Employeeobj();
Employeeobj f2 = new Employeeobj();
Employeeobj f3 = new Employeeobj();
for (int i = 0; i < 3) {
f(i).firstName = JOptionPane.showInputDialog("Please enter the first name of employee #1");
f(i).lastName = JOptionPane.showInputDialog("Please enter the last name of employee #1");
f(i).jobTitle = JOptionPane.showInputDialog("Please enter the job title of employee #1");
f(i).salary = Integer.parseInt(JOptionPane.showInputDialog("Please enter the salary of employee #1"));
f(i).age = Integer.parseInt(JOptionPane.showInputDialog("Please enter the age of employee #1"));
}

    } 
Arkaiid
  • 1
  • 2
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Silvio Mayolo May 21 '22 at 21:52
  • @SilvioMayolo I'm not sure what you mean... I'm not using arrays for this – Arkaiid May 21 '22 at 21:55
  • 1
    No, but you should be. What you have is three independent (unrelated) variables. If you want to act on them in a uniform way, then they need to be stored in some uniform way. – Silvio Mayolo May 21 '22 at 21:55
  • Aren't they not variables? they are a separate object, Employeeobj.java – Arkaiid May 21 '22 at 22:02
  • In your case: `Employeeobj[] employeeArray = new Employeeobj[3];` creates an array with 3 elements. Then initialize e.g. the first with `employeeArray[0] = new Employeeobj();` and so on - in your loop the same and access with `employeeArray[i].age` instead `f(i).age`. See [this tutorial](https://www.softwaretestinghelp.com/array-of-objects-in-java/). – hc_dev May 21 '22 at 22:11
  • Don't use [`JOptionPane`](https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html) if no Swing GUI application. Instead collect user-input over the console with your [`Scanner`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html), e.g. `String name = input.next();` or `int age = input.nextInt();`. Before prompt with `System.out.println("Enter age:");"` – hc_dev May 21 '22 at 22:17
  • @hc_dev thanks! also, the joptionpane is a choice. – Arkaiid May 27 '22 at 05:04

0 Answers0