java stringbuilder 删除一行

所以我有这个问题。我使用过 StringBuilder 并且我正在尝试使用 JOptionPane 创建一个显示函数,该函数将显示员工的姓名和薪酬。每次从系统中删除员工姓名时,显示功能还必须删除一行,因为我不希望 JOptionPane 框中有空行。但是,我无法正确显示它,因为当我尝试删除员工姓名时,它会删除它自己的员工姓名以及列表中它之前的所有员工姓名。StaffDetails 是一个单独的数组,我包含员工的姓名和工资。这是我显示员工的代码:


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

  } 

}


婷婷同学_
浏览 342回答 3
3回答

暮色呼如

我最好的建议是避免同时完成两个操作。在这里,您尝试过滤掉已删除的员工,同时将他们的数据附加到StringBuilder. 理想情况下,我会先过滤掉员工,然后使用他们来创建我的String输出。考虑到这一点,您的方法将如下所示:public static void display() {StringBuilder builder = new StringBuilder();List<Employee> filtered = Arrays.stream(employees)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(employee -> employee.getName() == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());filtered.forEach(employee ->&nbsp; &nbsp; builder.append(employee.getName())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.append("\t ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.append(employee.getPay())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.append("\n "));String output = builder.toString();JOptionPane.showMessageDialog(null,"Staff_No&nbsp; &nbsp; &nbsp; &nbsp;Staff_Name&nbsp; &nbsp; &nbsp; Pay&nbsp; &nbsp; &nbsp; \n" + output);}假设您将您的员工存储在一个Employees 数组中,我基本上会将该数组流式过滤掉所有名称为空的条目,并将结果收集到一个列表中。有了这个列表,我将继续使用StringBuilder来构建我的输出消息以显示在对话框中。还要注意这里,我认为让StringBuilder成为类的静态成员没有任何意义。你很可能在方法的上下文中拥有它。有了上述所有内容,您在这里有两个非常不同的操作。一个是过滤掉所有不需要的条目,另一个是输出字符串的实际构造。

慕妹3242003

只需检查null并跳过此员工。一开始staffnumber应该是0。此外,您不必StringBuilder创建静态成员,只需创建新的StringBuilder. (否则每次调用此方法时都必须清除它)public static void displayrezStaffRecord() {&nbsp; &nbsp; int staffnumber = 0;&nbsp; &nbsp; StringBuilder staffRecord= new StringBuilder();&nbsp; &nbsp; for (int n = 0; n < staffDetails.length; n++) {&nbsp; &nbsp; &nbsp; &nbsp; if (staffDetails[n].getStaffName() == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; menunumber--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; staffRecord.append(staffDetails[n].getStaffName());&nbsp; &nbsp; &nbsp; &nbsp; staffRecord.append("\t ");&nbsp; &nbsp; &nbsp; &nbsp; staffRecord.append(staffDetails[n].getPay());&nbsp; &nbsp; &nbsp; &nbsp; staffRecord.append(" \n ");&nbsp; &nbsp; &nbsp; &nbsp; staffnumber++;&nbsp; &nbsp; }&nbsp; &nbsp; String finalresult = staffRecord.toString();&nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Staff_No&nbsp; &nbsp; &nbsp; &nbsp;Staff_Name&nbsp; &nbsp; &nbsp; Pay&nbsp; &nbsp; &nbsp; \n" + finalresult);}我不知道是不是因为你的代码不完整,我看不到你在任何地方使用变量staffnumber。

收到一只叮咚

尝试仅删除当前员工并在删除后减少员工数量,如 if 语句中的以下内容:&nbsp; &nbsp;if (staffDetails[n].getStaffName()==null){&nbsp; &nbsp; &nbsp; menunumber--;&nbsp; &nbsp; &nbsp; staffRecord.delete(staffRecord.length()-4,staffRecord.length());&nbsp; &nbsp; &nbsp; staffnumber--;&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java