Search code examples
javastringbuilder

java stringbuilder deleting a line


So I have this problem. I have used StringBuilder and I am trying to create a display function using JOptionPane that will display a staff's name and pay. The display function also has to delete a line everytime a staff name is deleted from the system, as I do not want to have empty lines in my JOptionPane box. However, I cannot get it to display properly, as when I try to delete a staff name, it deletes it's own staff name, as well as all the staff names that are before it in the list. staffDetails is a separate array that i have containing the staff's name and pay. here is my code for displaying staff:

public static StringBuilder staffRecord= new StringBuilder();
  public static void displayrezStaffRecord(){
      int staffnumber=1;
      for (int n=0;n<staffDetails.length;n++){
          staffRecord.append(staffDetails[n].getStaffName());
          staffRecord.append("\t ");
          staffRecord.append(staffDetails[n].getPay());
          staffRecord.append(" \n ");
          staffnumber++;
          if (staffDetails[n].getStaffName()==null){
              menunumber--;
              staffRecord.delete(0,staffRecord.length());

          }

  }
      String finalresult= staffRecord.toString();
      JOptionPane.showMessageDialog(null,"Staff_No       Staff_Name      Pay      \n" +finalresult);
  } 
}

Solution

  • My best suggestion is to avoid having two operations done at the same time. Here your attempt to filter out your deleted employee while appending their data to the StringBuilder. Ideally, I would first filter out the employees and then use them to create my String output. With that in mind your method would look like:

    public static void display() {
    
    StringBuilder builder = new StringBuilder();
    
    List<Employee> filtered = Arrays.stream(employees)
                                    .filter(employee -> employee.getName() == null)
                                    .collect(Collectors.toList());
    
    
    filtered.forEach(employee ->
        builder.append(employee.getName())
               .append("\t ")
               .append(employee.getPay())
               .append("\n "));
    
    String output = builder.toString();
    JOptionPane.showMessageDialog(null,"Staff_No       Staff_Name      Pay      \n" + output);
    }
    

    Assuming that you store your employees in an array of Employees, I would basically, stream that array filtering out all entries with a null name, collecting the results in a list.

    With that list in hand, I would then go ahead to use the StringBuilder to construct my output message to be shown in the dialog. Note also here, that I do not see any point in having the StringBuilder be a static member of the class. You could very well have it within the context of the method.

    With all the above in place, you have two very distinct actions here. One is to filter out all the unwanted entries, and the other being the actual construction of the output string.