繁体   English   中英

Java StringTokenizer和存储数组

[英]Java StringTokenizer and storing Array

我已经为Worker编程了一个Worker类和驱动程序类。我的工作类正在正常编译并显示所需的输出。但是在数组和StringTokenizer中,我遇到了主要问题...在TestWorker.java的32行中一个错误,我不知道为什么会给出问题是...

(i)声明一个数组,该数组最多可以存储使用数据(名称,workerID和hourlyRate)构造的5个Worker对象的引用。

(ii)现在允许用户重复输入workerID和小时数,直到用户输入空字符串为止。 读取这些值并在适当的对象上调用方法addWeekly() (通过搜索数组以找到具有指定ID的对象)。 如果输入的ID不存在,则显示相应的错误消息。

(iii)计算并显示所有工人的工资

请在下面查看我的代码。

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

}

首先,当它实际上是此行上的方法时,您已将countTokens指定为字段:

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

现在您似乎将输入分割成一个+号? 因此,您期望输入的内容类似于s123+12 ,这意味着ID s123 worked 12 hours this week 因此,为了解决类似问题,您可能需要一个看起来像这样的循环:

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

请记住,这非常粗糙,从您的指示中我还不清楚您到底要实现什么目标。

为什么不考虑将为您提供字符串数组的String.split() name.split("\\\\+")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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