简体   繁体   中英

Java how to store JOptionPane value in array

hey so I have this assignment that requires me to create a mini program with a menu system that has options 1 and 2. Option 1 requires me to use JOptionPane to input and store people's names and their salaries using arrays (not arraylist). After that, option 2 will display the list of peoples names and salaries. However, I am unsure specifically on how to store user's input from JOptionPane into an array and later display it. Any help would be much appreciated! thank you!

    public class HR{  
    public static void getName(){
        String[] name= new String[20];
        for (int count=0;count<1;count++){
            name[count]= JOptionPane.showInputDialog(null,"Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
        } 
    } 
    public static void getSalary(){
        String[] salary= new String[20];
        for (int count=0;count<1;count++){
            salary[count]= JOptionPane.showInputDialog(null,"Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);
        } 
    } 

Using a single String array here is probably not ideal, as it holds just one String in each position, so you'd have to do something like this*:

String arr[] = new String[20];
arr[0] = JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
arr[1] = JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);

JOptionPane.showMessageDialog(null, "Name is: " + arr[0] + " and salary is " + arr[1]);

If 20 is your size, you can use a loop and add 10 entries of name and salary and print them after.

for (int i = 0; i < arr.length; ++i) {
    arr[i] = JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE);
    arr[++i] = JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE);
}

for (int i = 0; i < arr.length; ++i) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i] + " and salary is " + arr[++i]);
}

However, again you'd need to make sure the length of the array is an even number, and note the ++i instead of i++ here. Also, I'm still recommending using a different approach than a single String array for this problem. Even an array of your own custom class would work better.

So add your own class ie Person :

private String name;
private String salary;
//getters and setters

Define your array as:

Person arr[] = new Person[20];

Loop and set your values:

for (int i = 0; i < arr.length; ++i) {
    Person p = new Person();
    p.setName(JOptionPane.showInputDialog(null, "Please enter employee's name:", JOptionPane.QUESTION_MESSAGE));
    p.setSalary(JOptionPane.showInputDialog(null, "Please enter employee's salary:", JOptionPane.QUESTION_MESSAGE));
    arr[i] = p;
}

And print them:

for (int i = 0; i < arr.length; i++) {
    JOptionPane.showMessageDialog(null, "Name is: " + arr[i].getName() + " and salary: " + arr[i].getSalary());
}

*Based on your example code

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.sun.accessibility.internal.resources.accessibility;

import javax.swing.JButton;

public class stas {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    public static String salary_1="";
    public static String name_1="";

    public static String []name=new String[20];
    public static String []salary=new String[20];
    public static int counter=0;
    public static void get_name(String a)
{
    name[counter]=a;


}
    public static void get_salary(String a)
{
    salary[counter]=a;


}


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    stas window = new stas();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public stas() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("Enter Employe Name");
        lblNewLabel.setBounds(10, 47, 143, 20);
        frame.getContentPane().add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(198, 44, 86, 20);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        textField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
        name_1=textField.getText();


            }
        });

        JLabel lblNewLabel_1 = new JLabel("Enter Employe Salary");
        lblNewLabel_1.setBounds(10, 119, 114, 17);
        frame.getContentPane().add(lblNewLabel_1);

        textField_1 = new JTextField();
        textField_1.setBounds(198, 116, 86, 20);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        textField_1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                 salary_1=textField_1.getText();



            }
        });

        JButton btnNewButton = new JButton("Add Employe");
        btnNewButton.setBounds(168, 175, 116, 23);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                get_salary(salary_1);
                get_name(name_1);
                textField_1.setText("");
                textField.setText("");
                JOptionPane.showMessageDialog(null,"employe addedd");


            }
        });
       }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM