如果变量为空,如何删除行

时间:2019-01-15 14:20:58

标签: java netbeans-8

我目前正在学习如何打印内部带有数据的文本区域,但是在尝试想出一种方法来删除具有空值的行时遇到了麻烦

    String color1 = "";
    String color2 = "";
    String color3 = "";
    String color4 = "";
    String color5 = "";
    String color6 = "";
    String color7 = "";
    String color8 = "";
    int weight1 = 0;
    int weight2 = 0;
    int weight3 = 0;
    int weight4 = 0;
    int weight5 = 0;
    int weight6 = 0;
    int weight7 = 0;
    int weight8 = 0;


    TextArea.setText("COLOR\t\t:\t WEIGHT:" + 
            "\n=====================================\n" +
            color1 +"\t\t\t "+ weight1 + "\n" +
            color2 +"\t\t\t "+ weight2 + "\n" +
            color3 +"\t\t\t "+ weight3 + "\n" +
            color4 +"\t\t\t "+ weight4 + "\n" +
            color5 +"\t\t\t "+ weight5 + "\n" +
            color6 +"\t\t\t "+ weight6 + "\n" +
            color7 +"\t\t\t "+ weight7 + "\n" +
            color8 +"\t\t\t "+ weight8 );
}

通常,只有权重为color1和color2的值为值,很少达到color8的值。尽管color8及其权重没有值,但我如何确保仅当它具有值时才在文本区域中不显示它?

5 个答案:

答案 0 :(得分:1)

您可以将颜色和权重存储在LinkedHashMap中,但只能插入分配了权重的值。然后,您可以将条目转换为String

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("white", 2);
map.put("red", 1);

String text = map.entrySet()
                 .stream()
                 .map(e -> e.getKey() + "\t\t\t " + e.getValue() + "\n")
                 .collect(Collectors.joining());
System.out.println(text);

答案 1 :(得分:1)

我想您应该有一个值数组,并使用如下所示的方法检查是否存在颜色,如果存在,则将其添加到输出中。

int index=0;
StringBuilder result=new StringBuilder();
while(index<colors.length && Strings.isNotEmpty(colors[index]){
   result.append(colors[index]);
   result.append(DELIMITER); // \t\t\t 
   result.append(weights[index]);
   result.append("\n");
}
return result;

答案 2 :(得分:0)

如果您不想使用数组作为颜色,则可以检查每种颜色是否为空字符串:

TextArea.setText("COLOR\t\t:\t WEIGHT:" + 
            "\n=====================================\n" +
            !color1.equals("")?color1 +"\t\t\t "+ weight1 + "\n" +:"";
            !color2.equals("")?color2 +"\t\t\t "+ weight2 + "\n" +:"";
            !color3.equals("")?color3 +"\t\t\t "+ weight3 + "\n" +:"";
            !color4.equals("")?color4 +"\t\t\t "+ weight4 + "\n" +:"";
            !color5.equals("")?color5 +"\t\t\t "+ weight5 + "\n" +:"";
            !color6.equals("")?color6 +"\t\t\t "+ weight6 + "\n" +:"";
            !color7.equals("")?color7 +"\t\t\t "+ weight7 + "\n" +:"";
            !color8.equals("")?color8 +"\t\t\t "+ weight8:""; );

答案 3 :(得分:0)

public static void main(String[] args) {
            String color1 = "";
            String color2 = "";
            String color3 = "";
            String color4 = "";
            String color5 = "";
            String color6 = "";
            String color7 = "";
            String color8 = "";
            int weight1 = 0;
            int weight2 = 0;
            int weight3 = 0;
            int weight4 = 0;
            int weight5 = 0;
            int weight6 = 0;
            int weight7 = 0;
            int weight8 = 0;

            List<String> colors= Arrays.asList(color1, color2 , color3 , color4 , color5 , color6 , color7 , color8);
            List<String> colors2=  colors.stream().filter(p -> colors.isEmpty()).collect(Collectors.toList());

            List<Integer> weights = Arrays.asList(weight1, weight2, weight3, weight4, weight5, weight6, weight7, weight8);

            stamp(colors2,weights);

    }

   private static void  stamp (List<String> colors, List<Integer> weights) {


       String header ="COLOR\t\t:\t WEIGHT:" + 
            "\n=====================================\n";

       String row = "";
       int i = 0;
       for (String color : colors) {
           row = row + color +"\t\t\t "+ weights.get(i)+ "\n";
           i++;
       }
        System.out.println(header+row);
   }

答案 4 :(得分:0)

尝试这种方式

List<String> colors = new ArrayList<>();
List<Integer> weights = new ArrayList<>();

String text = "COLOR\t\t:\t WEIGHT:" + 
          "\n=====================================";
if (!colors.isEmpty() && !weights.isEmpty()) {
  for (String color : colors) {
      if (weights.get(colors.indexOf(color) != null) {
         text = text + "\n" + color +"\t\t\t "+ weights.get(colors.indexOf(color));
      }
  }
}

TextArea.setText(text);
相关问题