Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:29:06+00:00 2026-06-12T23:29:06+00:00

I have programmed a Worker class and the driverclass for Worker.. My working class

  • 0

I have programmed a Worker class and the driverclass for Worker..
My working class is compiling fine and showing the desired output..
But in array and StringTokenizer I am facing the main problem… And in line 32 of TestWorker.java there is an error, and I donot know why is it giving
The question is…

(i)Declare an array that can store the references of up to 5 Worker objects constructed with the data(name,workerID and hourlyRate).

(ii)And now allow the users to enter workerID and hours repeatedly until user enter an empty string. Read these values and invoke the method addWeekly() on the appropriate object (by searching through the array to locate the object with the specified ID). If a nonexistent ID is entered, display an appropriate error message.

(iii)Compute and display the salary for all the workers

Please see my codes below…..

//Worker.java

public class Worker { 
        public final double bonus=100;    
        protected String name, workerID;
        protected double hourlyRate, totalHoursWorked,tax,grossSalary,netSalary;

        public Worker(){

    }
public Worker(String name, String workerID, double hourlyRate){
    this.name = name;
    this.workerID = workerID;
    this.hourlyRate = hourlyRate;

    }

public void addWeekly(double hoursWorked){
    this.totalHoursWorked = this.totalHoursWorked + hoursWorked;
    }

public double gross(){
    grossSalary = (totalHoursWorked*hourlyRate);
            if(totalHoursWorked>=150){
        grossSalary = grossSalary +100;
        }
        return  grossSalary;
        }
public double netAndTax(){
    netSalary = grossSalary;
    if(grossSalary>500){
        tax = (grossSalary - 500) *0.3;
        netSalary = (grossSalary - tax);

    }
    return netSalary;
 }
public String getName(){
    return this.name;
}

public String getWorkerID(){
    return this.workerID;
}

public double getHourlyRate(){
    return this.hourlyRate;
}

public double getTotalHours(){
    return totalHoursWorked;
}

public double getGrossSalary(){
    return grossSalary;
    }

public void addToGross(double amt){
    grossSalary = grossSalary + amt;
}
public void displaySalary(){
    System.out.print("Name: " +getName() + "\nID :" + getWorkerID() 
            + "\nHourly Rate: " + getHourlyRate()+ "\nTotalHours Worked" + getTotalHours() + 
            "\nGross pay" + getGrossSalary() + "\nTax: " + netAndTax() + 
            "\nNet Pay: " + netAndTax());
}

}

//TestWorker.java

import java.util.Scanner;
import java.util.StringTokenizer;
public class TestWorker {
    public static void main(String args[]){
        Worker e = new Worker("John Major","s123",15.0);
        e.addWeekly(45);
        e.addWeekly(40);
        e.addWeekly(32);
        e.addWeekly(38);
        e.gross();
        e.netAndTax();
        e.displaySalary();

        Worker[] worklist = new Worker[5];
        worklist[0] = new Worker("Richard Cooper","s1235",20.0);
        worklist[1] = new Worker("John Major","s1236",18.0);
        worklist[2] = new Worker("Mary Thatcher","s1237",15.0);
        worklist[3] = new Worker("David Benjamin","s1238",16.0);
        worklist[4] = new Worker("Jack Soo","s1239",25.0);

    Scanner input = new Scanner(System.in);
    String name;
    double hourly;

    do{
        System.out.println("\n"); 
        System.out.print("Please Enter ID and hours-worked in a given week: ");
        name = input.nextLine();

        StringTokenizer string = new StringTokenizer(name,"+");
        String[] array  =new String[(string.countTokens)];
        for(int i=0;i<=4;i++){
            if(array[0].equals(worklist[0])){
            e.getName();
            e.getWorkerID();
            e.addWeekly(Double.parseDouble(array[0]));
            e.gross();
            e.displaySalary();

            }
            else if(array[0].equals(worklist[1])){
            e.getName();
            e.getWorkerID();
            e.addWeekly(Double.parseDouble(array[1]));
            e.gross();
            e.displaySalary();
            }

            else if(array[0].equals(worklist[2])){
            e.getName();
            e.getWorkerID();
            e.addWeekly(Double.parseDouble(array[2]));
            e.gross();
            e.displaySalary();
            }

            else if(array[0].equals(worklist[3])){
            e.getName();
            e.getWorkerID();
            e.addWeekly(Double.parseDouble(array[3]));
            e.gross();
            e.displaySalary();
            }

            else if(array[0].equals(worklist[4])){
            e.getName();
            e.getWorkerID();
            e.getHourlyRate();
            e.addWeekly(Double.parseDouble(array[4]));
            e.gross();
            e.displaySalary();
            }

            else{
            System.out.println("Please Enter correct information");
        }

     }

        System.out.println();
        while(string.hasMoreElements()){
            System.out.println(string.nextElement());
        }
    }
    while(name.isEmpty()==false);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:29:08+00:00Added an answer on June 12, 2026 at 11:29 pm

    Firstly, you’ve specified countTokens as a field when it is actually a method on this line:

    String[] array = new String[(string.countTokens)]; // Incorrect
    String[] array = new String[(string.countTokens())]; // Correct
    

    Now you seem to be splitting the input on a + sign? So you’re expected input is something along the lines of s123+12 which would mean ID s123 worked 12 hours this week. So in order to solve something like you probably want a loop that looks like this:

    Scanner input = new Scanner(System.in);
    
    System.out.println("\n");
    System.out.print("Please Enter ID and hours-worked in a given week: ");
    String enteredString = input.nextLine();
    
    while (!enteredString.isEmpty()) {
        StringTokenizer stringtok = new StringTokenizer(enteredString, "+");
        String id = stringtok.nextToken();
        Double hours = Double.parseDouble(stringtok.nextToken());
    
        for (int i = 0; i < 5; i++) {
            if (worklist[i].getWorkerID().equals(id)) {
                worklist[i].addWeekly(hours);
                break;
            }
        }
    
        System.out.println("\n");
        System.out.print("Please Enter ID and hours-worked in a given week: ");
        enteredString = input.nextLine();
    }
    
    for (int i = 0; i < 5; i++) {
        worklist[i].gross();
        worklist[i].netAndTax();
        worklist[i].displaySalary();
    }
    

    Keep in mind that this is very rough and I’m not entirely clear from your instructions on what exactly you are trying to achieve.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have programed a Worker and MachineWorker class. It complies fine. but when i
I have programmed an emulator, but I have some doubts about how to organizate
I have never programmed a gadget for Vista or Seven, but I would like
I have worked with my self programmed CMS but new on Umbraco. I am
I have programmed a shopping list. The footer contains buttons for actions on a
I have programmed in HTML, Java, CSS, C++, VB, an Python. I'm looking to
Once I have programmed GUI with Java and have used Form Layouts. Form layout
I have never programmed in visual basic before and my boss just told me
I have a webservice programmed in coldfusion which I'm attempting to consume using c#.net.
I have a Windows Communication Foundation (WCF) service programmed in C#. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.