-3

Is there anyway I can create a loop for System.out.println("...") and reading in the values? For example I wish to do this:

    System.out.println("Input the Executive's name: ");
    String eName = input.nextLine();
    System.out.println("Input the Executive's address: ");
    String eAddress = input.nextLine();
    System.out.println("Input the Employee's name: ");
    String emName = input.nextLine();
    System.out.println("Input the Employee's address: ");
    String emAddress = input.nextLine();

However, I want to do it for volunteers, staff, human resources, and miscellaneous staff. I don't want to create so many System.out.println and have to read so many variables in. Is there any way I can create a loop or an array to make this more clean and concise?

1 Answer 1

1

Use map with key value pairs.

Give a try to this:

java.util.Map<String,String> contacts = new java.util.HashMap<>();
for (String key : new String[]{"Executive's name", "Executive's address", "Employee's name", "Employee's address"}){
    System.out.println("Input the " + key + ": ");
    contacts.put(key, input.nextLine());
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.