Reputation: 43
I am currently learning how to print a textarea with data inside, but am having trouble trying to think of a way to remove lines which have empty values
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 );
}
Usually, only color1 & color2 with their weights have a value, very rarely does it reach color8. Although color8 & its weight does not have a value how can I make sure it is not displayed in the textarea only if it has a value?
Upvotes: 0
Views: 155
Reputation: 31
Try this way
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);
Upvotes: 0
Reputation: 174
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);
}
Upvotes: 0
Reputation: 723
I suppose that you should have an array of values and use a method like the following where you check if there exists a color, if yes then add to the output.
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;
Upvotes: 1
Reputation: 1149
If you don't want to use arrays for the colors, you can check if each color is an empty string:
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:""; );
Upvotes: 0
Reputation: 44960
You can store colors and weights in a LinkedHashMap
but only insert the values that have a weight assigned. You can then convert entries into a 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);
Upvotes: 1