thalia delmoro
thalia delmoro

Reputation: 25

How to separate several inputs on a jtextfield

I am doing a Wage Calculator that will calculate a worker's salary according to their position as the boss, commission worker and such but i need it to accept 5 inputs each textfield for the First name, Last name, and salary. How do i seperate each inputs from each textfields?

Example :
First Name: Boss, Boss, Boss, Boss, Boss
Last Name: A, B, C, D, E
Salary: 1, 2, 3, 4, 5

Output:
Boss A earned 1
Boss B earned 2
Boss C earned 3
Boss D earned 4
Boss E earned 5

Here's a part of my code, this can only print 1 input

public void actionPerformed(ActionEvent e)
{
    //BOSS
    if (e.getSource() == fldSalary)
    {
        String first = fldFirst.getText();
        String last = fldLast.getText();
        double salary = Double.parseDouble(fldSalary.getText());


        Boss boss = new Boss(first,last,salary);

        employee = boss;
        output = employee.toString() + " earned Php" + precision2.format(employee.earnings()) + "\n \n" ;
        area.append(output);
    }  
}

Upvotes: 1

Views: 1630

Answers (1)

Zied R.
Zied R.

Reputation: 4954

use split function

String last= fldLast.getText();  // last=" A, B, C, D, E";
String[] parts = last.split(", ");
String part1 = parts[0]; // A
String part2 = parts[1]; // B
String part3 = parts[2]; // C
String part4 = parts[3]; // D
String part5 = parts[4]; // E

Same for first and salary (Salary should be string, then parse each one to double)

Upvotes: 3

Related Questions