0

updateEmployees(PersonnelManager pm) 方法读取一个文本文件,并根据文件中每行的第一个字符(有 3 种可能性)执行不同的代码。PersonnelManager 和 Employee 类在问题中没有任何作用,这就是为什么我没有将它们包括在这里。这是一个示例输入文件:

n Mezalira, 卢卡斯 h 40000

r 5

d 金赛

n Pryce, Lane s 50

r 4

这是方法:(文件和扫描器对象在方法之外声明)

public static boolean updateEmployees(PersonnelManager pm) {
    
    try 
    {
    file = new File(updates);
    in = new Scanner(file);
    }
    
    catch (FileNotFoundException e) 
    {
        System.out.println("Could not load update file.");
        return false;
    }
    
    int currentLine = 1; //Keep track of current line being read for error reporting purpose
    while (in.hasNext()) {
        String line = in.nextLine();
        
        //Check the type of update the line executes
        
        //Update: Add new Employee
        if (line.charAt(0) == 'n') {
    
            String[] words = line.split("\\s"); //Split line into words. Index [0]: update type.  [1]: last name.   [2]: first name.   [3]: employee type.  [4]: wage.
            words[1] = words[1].substring(0, words[1].length() - 1); //remove comma from last name
    
            if (words.length != 5) { //If there are not 5 words or tokens in the line, input is incorrect.
                System.out.println("Could not update. File contains incorrect input at line: " + currentLine);
                return false; 
            }
            
            if (words[3].equals("s"))  //if employee is type SalariedEmployee
                pm.addEmployee(new SalariedEmployee(words[2], words[1], Double.parseDouble(words[4])));
            
            
            else if (words[3].equals("h"))  //if employee is type HourlyEmployee
                pm.addEmployee(new HourlyEmployee(words[2], words[1], Double.parseDouble(words[4])));
            
            else {
                System.out.println("Could not update. File contains incorrect input at line: " + currentLine);
                return false;
            }
            //Display information on the console
            System.out.println("New Employee added: " + words[1] + ", " + words[2]);
        }
        
        //Update: Raise rate of pay
        if (line.charAt(0) == 'r') {
            String[] words = line.split("\\s"); //Split line into words. Index [0]: update type.  [1]: rate of wage raise
            if (Double.parseDouble(words[1]) > 100.0) { //If update value is greater than 100
                System.out.println("Error in line:" + currentLine + ". Wage raise rate invalid.");
                return false;
            }
            
            for (int i =0; i<pm.howMany(); i++) { //Call raiseWages() method for all employees handled by the pm PersonnelManager
                pm.getEmployee(i).raiseWages(Double.parseDouble(words[1]));
            }
            
            //Display information on the console
            System.out.println("New Wages:");
            pm.displayEmployees();
            
        }
        
        //Update: Dismissal of Employee
        if (line.charAt(0) == 'd') {
            String[] words = line.split("\\s"); //Split line into words. Index [0]: update type.  [1]: last name of employee
            if (words.length != 2) { //If there are not 2 words or tokens in the line, input is incorrect.
                System.out.println("Could not update. File contains incorrect input at line: " + currentLine);
                return false; 
            }
            
            String fullName = pm.getEmployee(words[1]).getName(); //Get complete name of Employee from last name
            pm.removeEmployee(words[1]);
            
            //Display information on the console
            System.out.println("Deleted Employee: " + fullName);
        }
        
        currentLine++;
    }
    return true;
}

由于输入文件中有 5 行,while 循环应该执行 5 次,但事实并非如此。当它到达输入文件中的第 4 行:“n Pryce, Lane s 50”时,在代码的第 25 行出现“java.lang.StringIndexOutOfBoundsException”错误。

问题出现在第 24 和 25 行:

String[] words = line.split("\\s"); //Split line into words. Index [0]: update type.  [1]: last name.   [2]: first name.   [3]: employee type.  [4]: wage.
words[1] = words[1].substring(0, words[1].length() - 1); //remove comma from last name

对于输入的第 4 行,“line”字符串没有按照应有的方式分成 5 个字符串。它只被拆分为一个,在 words[0] 中,它等于“n”。我不明白的是程序使用同一行代码来拆分前 3 行输入的字符串,为什么它在第 4 行不起作用?

当我将输入文件更改为

n Mezalira, 卢卡斯 h 40000

r 5

d 金赛

删除命令“n”的第二次出现,它可以工作。事实上,每次我多次使用使用相同命令(“n”、“r”或“d”)的输入文件时,第二次出现该命令的行只会被拆分为 1 个字符串。包含行上的第一个标记(在这种情况下为“n”、“r”或“d”)。

我希望我的解释很清楚。如果有人知道为什么会这样,请帮忙。

4

1 回答 1

2

您的split()电话实际上应该是split("\\s+")允许字段之间有多个空格。

于 2013-09-07T03:54:51.510 回答