Note that there are some explanatory texts on larger screens.

plurals
  1. POJava split() method won't work a second time
    primarykey
    data
    text
    <p>The method updateEmployees(PersonnelManager pm) reads a text file and depending on the first character of each line (there are 3 possiblities) on the file, it executes different code. The PersonnelManager and Employee classes have no play in the problem, that's why I'm not including them here. Here is a sample input file:</p> <blockquote> <p>n Mezalira, Lucas h 40000</p> <p>r 5</p> <p>d Kinsey</p> <p>n Pryce, Lane s 50</p> <p>r 4</p> </blockquote> <p>Here is the method: (the File and Scanner objects are declared out of the method)</p> <pre><code>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]) &gt; 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&lt;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; } </code></pre> <p>Since there are 5 lines in the input file, the while loop should execute 5 times, but that's not what happens. when it reaches the 4th line in the input file: "n Pryce, Lane s 50", I get an "java.lang.StringIndexOutOfBoundsException" error, in line 25 of the code.</p> <p>The problem occurs at lines 24 and 25:</p> <pre><code>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 </code></pre> <p>For the 4th line of input, the "line" String is not split in 5 Strings as it should. It is only split into one, which is in words[0] and it equals "n". What I don't understand is the program used the same line of code to split the String for the first 3 lines of input, why doesn't it work on the 4th line?</p> <p>When I change the input file to</p> <blockquote> <p>n Mezalira, Lucas h 40000</p> <p>r 5</p> <p>d Kinsey</p> </blockquote> <p>removing a second occurrence of the command "n", it works. In fact, every time I use an input file that uses the same command ("n", "r" or "d") more than once, the line where the command happens the second time will only be split into 1 string. containing the first token ( int this case "n", "r", or "d") on the line.</p> <p>I hope I was clear in my explanation. If anyone know why this happens, please help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload