title: StringTokenizer and Array Java
body: <p>I am completed the whole program, and it is working fine. But, I am having trouble with the StringTokenizer. I have class named Worker.java and a test class named TestWorker.java...</p>
  
  <p>In the test class I have calculated and printed out the total hoursworked, grossSalary and etc..
  But I am stuck in 1 position, that is I have to store name,id, and hourly rate in array, and then using the StringTokenizer, I have to prompt the user to enter worker ID and hourly rate.</p>
  
  <p>When the user enter the user worker name and ID, the program output will show the user the hourly rate of the worker...</p>
  
  <p>or in other words...</p>
  
  <p>Allow the user to enter worker-ID and Hours repeatedly until user enter and empty string. Read the values and invoke the methods 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.</p>
  
  <p>Please see my codings below and modify as your needs....</p>
  
  <pre><code>//Worker.java
  
  public class Worker {
      public final double bonus=100;
      protected String name;
      protected String workerID;
      protected double hourlyRate;
      protected double hoursWorked;
      protected double weekHour;
      protected double tax;
      protected double grossSalary;
      protected double netSalary;
  
  
      public Worker() {
      }
  
      public Worker(String name,String workerID,double hourlyRate){
          this.name = name;
          this.workerID = workerID;
          this.hourlyRate = hourlyRate;
      }
  
      public void addWeekly(double weekHour){
  
          hoursWorked = hoursWorked + weekHour;
  
      }
  
      public double gross()
      {
          grossSalary = (hoursWorked*hourlyRate);
          if(hoursWorked&gt;=150)
          {
              grossSalary = grossSalary +100;
          }
          return  grossSalary;
      }
  
      public double taxAndNet()
      {
          tax = (grossSalary - 500) * 0.3;
          netSalary = (grossSalary-tax);
          return netSalary;
      }
  
      public String getName()
      {
          return name;
      }
  
      public String getWorkerID()
      {
          return workerID;
      }
  
      public double getHourly()
      {
          return hourlyRate;
      }
  
      public double getTotalHours()
      {
          return hoursWorked;
      }
  
      public double getGrossSalary()
      {
          return grossSalary;
      }
  
      public void addToGross(double amt)
      {
          grossSalary = grossSalary + amt;
      }
  
      public void displaySalary()
      {
  
          System.out.println("Name :" + name + "\nID :" + workerID +"\nHourly rate : "+ hourlyRate +"\nTotal Hours Worked " + hoursWorked +"\nGross Pay: "+getGrossSalary()+ "\nTax : "+tax +"\nNetpay :" +netSalary);
      }
  
  }
  
  
  //TestWorker.java
  
  import java.util.StringTokenizer;
  import java.util.Scanner;
  
  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.taxAndNet();
          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;
  
          do{
              System.out.print("Please enter your name and worker ID: ");
              name = input.nextLine();
  
              StringTokenizer string = new StringTokenizer(name,"+");
              System.out.println("******|||||*******");
              while(string.hasMoreElements()){
                  System.out.println(string.nextElement());
              }
          }
          while(name.isEmpty()==false);
  
           String s = "Five+Three=Nine-One";
          String arr[];
  
            //declare it with 3 tokens as seen above:
        StringTokenizer st = new StringTokenizer(s, "+=-");
  
        //the array size is the number of tokens in the String:
        arr = new String[st.countTokens()];
  
            //when there are still more tokens, place it in the array:
        int i = 0;
            while(st.hasMoreTokens()){
          arr[i] = st.nextToken();
                  i++;
            }
  
        //determine the word with the largest length:
            int indexMax = 0;
            for( i = 1; i &lt; arr.length; i++){
               if(arr[i].length() &gt; arr[indexMax].length())
              indexMax = i;
            }
  
        System.out.println("The largest element is in index: "
                  + indexMax);
  
    } //main
  } //class
  </code></pre>