3XSTACY
3XSTACY

Reputation: 1

How do I save the input data received by JOptionPane?

I'm busy studying Javascript as my first language, so I am very new. I am busy with a project for generating company e-mails and passwords, and I want to use JOptionPane to show a pop-up for the user to enter their details e.g. first name, last name etc.

I want to use the information, that was entered, to generate an e-mail for that specific employee. How would I do this?

This is how I wrote it to see that I can generate e-mail addresses correctly. How would I utilise JOptionPane in this?

public Email(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;


    this.department = setDepartment();
    System.out.println("Department: " + this.department);


    this.password = randomPassword(defaultPasswordLength);
    System.out.println("Your password is: " + this.password);

    email = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@" + department + companySuffix;
    System.out.println(email);
}

Upvotes: 0

Views: 965

Answers (1)

cseegm
cseegm

Reputation: 41

I am also new to Java, so there are probably better answers than mine, but I know that to use JOptionPane you first import javax.swing.JOptionPane; then you create the variable name and set it equal to an input dialog box. The input will be stored under the variable name as a string that you can call later in the program.

Remember that if you ever want the user to input a number, the input will still be stored as a string and you will have to use a parse method to convert it to a number.

import javax.swing.JOptionPane;

public class JavaApplication1 {

    public static void main(String[] args) {
        String firstName = JOptionPane.showInputDialog("Type your first name");
        String lastName = JOptionPane.showInputDialog("Type your last name");
        JOptionPane.showMessageDialog(null, "Your email is: "
            + firstName.toLowerCase() + "." + lastName.toLowerCase() 
            + "@email.com");
    }
}

Upvotes: 1

Related Questions