0

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);
        } 
    } 

2 Answers2

0

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 i.e. 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

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • thanks! however, since i need to add multiple entries in succession, would this still work? since arr[0] and arr[1] are hard coded. – Monet Walkens Jul 23 '18 at 14:18
  • Not like this no; you'd need a loop and use the indexes in place of the hardcoded positions. However, this isn't an ideal scenario for a single String array, you might need a 2d array, or even better, a Map or List. – achAmháin Jul 23 '18 at 14:23
  • @MonetWalkens I've given you a better solution there. – achAmháin Jul 23 '18 at 14:37
0
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");


            }
        });
       }
     }
  • Ok no problem its the same you will place this piece of code in the place you want – Hummel Goodspeed Hummelgoodspe Jul 23 '18 at 14:21
  • yeap! just tested it. sorry that I didnt state it clearly in my post but the JOptionPane runs 20 times which is not what I want. I need a system where i can add a employee's name then salary, loop to the main menu, and add a employee's name and salary again. – Monet Walkens Jul 23 '18 at 14:24
  • lets sum this up what you need is to add employe name and salary lets say n times for n employes without poping up the joptionpane n times ? – Hummel Goodspeed Hummelgoodspe Jul 23 '18 at 14:26
  • What I have is a menu system, that has 2 options. 1st option to add employee name and salary and the 2nd option is to view employee's name and salary. When I add a employee's name and salary the JOptionPane pops up one after another respectively. After adding the name and salary, it loops back to the main menu, and i can display the result by pressing Option 2. however, if i want to add more employee's names and salaries, i can do so by pressing option 1 again and repeating the whole process again. – Monet Walkens Jul 23 '18 at 14:29
  • you could use an array list for every employes instead of using 2 string arrays this array list will store employe name and salary what you need to do is to create only 2 joptionpanes in the main and loop on it 20 times every time you get the employe name and salary send it to the get name & get salary functions but take care to indicate the array list as global in order not to add from index 0 every time you call the 2 functions or ypu can just call 1 function that will store the name and salary – Hummel Goodspeed Hummelgoodspe Jul 23 '18 at 14:31
  • Yes, but the problem is that I am not allowed to use ArrayList for this question. – Monet Walkens Jul 23 '18 at 14:33
  • No problem Accept the answer if you found it useful Happy Coding – Hummel Goodspeed Hummelgoodspe Jul 23 '18 at 14:50
  • `frame.getContentPane().setLayout(null);` -> don't use `null` [layout](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing). Also, not sure how the OP prints his users with this, unless I'm missing something. – achAmháin Jul 23 '18 at 14:53