#Switch statement + Arrays

1 messages · Page 1 of 1 (latest)

versed yarrow
#

I have the following code:

    public void amountOfDepartments() {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Amount of departments to create: ");
        int amountOfDepartments = keyboard.nextInt();
        keyboard.nextLine();

        this.departments = new String[amountOfDepartments];
        for(int i = 0; i < amountOfDepartments-1; i++) {
            System.out.print("Enter department name: ");
            departments[i] = keyboard.nextLine();
        }
    }```

```java
    public void setDepartment(Person person) {
        int departmentChoice;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\nNew Employee: " + person.getFirstName());
        Arrays.stream(departments).forEach(System.out::println);
        System.out.print("\nEnter department code: ");
            departmentChoice = keyboard.nextInt();
            switch (departmentChoice) {
                case 1:
                    this.department = departments[0];
                    person.setEmail(person.getFirstName().toLowerCase() + "." + person.getLastName().toLowerCase() + "@" + this.department + companySuffix);
                    break;
                case 2:
                    this.department = "development";
                    person.setEmail(person.getFirstName().toLowerCase() + "." + person.getLastName().toLowerCase() + "@" + this.department + companySuffix);
                    break;
                case 3:
                    this.department = "accounting";
                    person.setEmail(person.getFirstName().toLowerCase() + "." + person.getLastName().toLowerCase() + "@" + this.department + companySuffix);
                    break;
                default:
                    System.out.println("Incorrect input, try again.\n");
        }
    }```

Pay no attention the the last bit of code, I'm still in the midst of editing.
My question is if it's possible to add the amount of cases predefined in the first method "amountOfDepartments" automatically? Not sure how to explain this better..

For example: if I would call the method amountOfMethods and I select 5 and add them accordingly, the call to method setDepartment should automatically have assigned 5 cases to the switch statement. Is this something that is possible with a switch statement? If not, what's a better way to accomplish this ?
deft raftBOT
# versed yarrow I have the following code: ```java public void amountOfDepartments() { ...

Detected code, here are some useful tools:

Formatted code
public void amountOfDepartments() {
  Scanner keyboard = new Scanner(System.in);
  System.out.print("Amount of departments to create: ");
  int amountOfDepartments = keyboard.nextInt();
  keyboard.nextLine();
  this .departments = new String[amountOfDepartments] ;
  for (int i = 0; i < amountOfDepartments - 1; i++) {
    System.out.print("Enter department name: ");
    departments[i]  = keyboard.nextLine();
  }
}
#

<@&987246399047479336> please have a look, thanks.

deft raftBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

distant rover
#

if you really want to have this switch statement code generated you would need some weird reflection code or by writing a java file which overrides your current one, but you could do a custom switch by getting from user the department amount, department name. And then when you want to get any of the code executed the user need to enter the choice between the departments

#

check if the input of the user match any of pre defined from user departments and then send the mail

versed yarrow
distant rover
#

Like enter Amount of the departments, then a new array will get created with that size, then with a for loop the user enters the names, this will be saved in that String array. Now when user want to send a email to any of the departments he just need to type the department name(or number of it) and then check if this input match any of the departments in the String array, if it do send the email to lastname.firstname@String[which got choose] or whatever

#

if you want to have more information last firstname, lastname, department you can use a generics to save many things in one department

#

english suck again, i hope u understand

#

something like that: ```Java
private String[] departments;

public void amountOfDepartments() {
    Scanner scan = new Scanner(System.in);
    System.out.print("Amount of departments: ");
    int amountOfDepartments = scan.nextInt();
    scan.nextLine();

    this.departments = new String[amountOfDepartments];
    for(int x = 0; x < amountOfDepartments; x++) {
        System.out.print("Enter department name: ");
        departments[x] = scan.nextLine();
    }
}

public void setDepartment(Person person) {
    int departmentChoice;
    Scanner scan = new Scanner(System.in);
    System.out.print("\nNew Employee: " + person.getFirstName());
    for(int x = 0; x < departments.length; x++) 
        System.out.println((x+1) + ". " + departments[i]);
    System.out.print("\nEnter department code: ");
    departmentChoice = scan.nextInt();
   scan.nextLine();

    if(departmentChoice >= 1 && departmentChoice <= departments.length) {
        this.department = departments[departmentChoice-1];
        person.setEmail(person.getFirstName().toLowerCase() + "." + person.getLastName().toLowerCase() + "@" + this.department + companySuffix);
    } else 
        System.out.println("Incorrect input, try again.\n");
}```
#

If you want to get name and lastname as input too, use 3D array or HashMap or many Arrays

versed yarrow
#

I'm doing something wrong, I keep getting NPE

#
public void showMenu() {
        Scanner keyboard = new Scanner(System.in);
        boolean flag = true;
        while(flag) {
            System.out.print(
                    "\n1. Show info" +
                    "\n2. Set amount of departments" +
                    "\n3. Set department of employees" +
                    "\n4. Change password\n");
            System.out.print("Please select an option: ");
            switch (keyboard.nextInt()) {
                // Show info
                case 1:
                    showInfo();
                    break;
                // Call a method asking for the amount of departments
                case 2:
                    department.amountOfDepartments();
                    break;
                // Call a method asking for the department - return the department
                case 3:
                    department.setDepartment();
                    break;
                // Call a method to change the password
                case 4:
                    changePassword();
                    break;
                default:
                    System.out.println("Incorrect input, try again: "); flag = true;
            }
        }
    }```

I assume it's because department is still null here so it's trying to call setDepartment method on null ?
distant rover
#

initialize the department before you call showMenu

versed yarrow
#
public class Main {
    public static void main(String[] args) {
        Employee employee = new Employee("Agon", "Test");
        Department department = new Department();
        Email email = new Email(employee, department);
        email.showMenu();
    }
}```
#

Is this not correct? hmmmm

#

Exception in thread "main" java.lang.NullPointerException
at playaroundwithobjects.Department.setDepartment(Department.java:31)
at playaroundwithobjects.Email.showMenu(Email.java:110)
at playaroundwithobjects.Main.main(Main.java:8)

versed yarrow
#

I figured it out, I had to add a constructor of Department and pass the employee as a parameter

#

It's working now, the only issue is that it's not saving the email

deft raftBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

versed yarrow
#

Anyone any ideas?

#
public void setDepartment() {
        int departmentChoice;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nEmployee: " + employee.getFirstName() + " " + employee.getLastName());

        for(int i = 0; i < departments.length; i++)
            System.out.println((i+1) + ". " + departments[i]);
        System.out.print("\nEnter department code: ");
        departmentChoice = keyboard.nextInt();
        keyboard.nextLine();

        if(departmentChoice >= 1 && departmentChoice <= departments.length) {
            this.department = departments[departmentChoice-1];
            employee.setEmail(employee.getFirstName().toLowerCase() + "." + employee.getLastName().toLowerCase() + "@" + this.department + companySuffix);
        } else
            System.out.println("Incorrect input, try again.\n");
    }```
deft raftBOT
# versed yarrow ```java public void setDepartment() { int departmentChoice; Scan...

Detected code, here are some useful tools:

Formatted code
public void setDepartment() {
  int departmentChoice;
  Scanner keyboard = new Scanner(System.in);
  System.out.println("\nEmployee: " + employee.getFirstName() + " " + employee.getLastName());
  for (int i = 0; i < departments.length; i++) System.out.println((i + 1) + ". " + departments[i] );
  System.out.print("\nEnter department code: ");
  departmentChoice = keyboard.nextInt();
  keyboard.nextLine();
  if (departmentChoice >= 1 && departmentChoice <= departments.length) {
    this .department = departments[departmentChoice - 1] ;
    employee.setEmail(employee.getFirstName().toLowerCase() + "." + employee.getLastName().toLowerCase() + "@" + this .department + companySuffix);
  }
  else System.out.println("Incorrect input, try again.\n");
}
versed yarrow
#

Never mind I found it myself

#

Can close, tyvm!