I made this program to calculate set of numbers
public void himpunanIrisan(){
System.out.print("The Result is {");
for(int k=1; k<=setofB; k ){
boolean Sama = false;
for(int i=1; i<=setofA; i ){
if(numberB[k] == numberA[i]){
Sama = true;
}
} if (Sama==true){
System.out.print(numberB[k] ",");
}
} System.out.println("}");
}
and the output will show like this
The Result is {1,2,}
is there any way so the other comma will dissepear?
CodePudding user response:
Change this line:
System.out.print(numberB[k] ",");
to this should work.
System.out.print(numberB[k] (k < setofB ? "," : ""));
CodePudding user response:
Using a joining collector would be easier and help you avoid reinventing the wheel. Additionally, converting the arrays to Set
s would perform better than a nested loop over two arrays (O(m n) instead of O(m*n), where m and n are the sizes of the two array):
Set<Integer> setB = stream(numberB).boxed().collect(Collectors.toSet());
System.out.println(
Arrays.stream(numberA)
.boxed()
.filter(setB::contains)
.map(Object::toString)
.collect(Collectors.joining(",", "{", "}"))
);
CodePudding user response:
Idea from How to delete stuff printed to console by System.out.println()?
You can use
System.out.print("\b");
To delete the "," character from STDOUT (see link for how to make it work in eclipse). Or you can change the code to something like this for example:
public void himpunanIrisan(){
boolean isFirst = true;
System.out.print("The Result is {");
for(int k=1; k<=setofB; k ){
boolean Sama = false;
for(int i=1; i<=setofA; i ){
if(numberB[k] == numberA[i]){
Sama = true;
}
}
if (Sama==true){
if(isFirst) {
System.out.print(numberB[k]);
isFirst = false;
} else {
System.out.print("," numberB[k]);
}
}
} System.out.println("}");
}
CodePudding user response:
The StringJoiner
is really convenient for such cases.
public void himpunanIrisan(){
StringJoiner joiner = new StringJoiner(",");
for(int k=1; k<=setofB; k ){
boolean sama = false;
for(int i=1; i<=setofA; i ){
if(numberB[k] == numberA[i]) sama = true;
}
if(sama) joiner.add(numberB[k]);
}
System.out.printf("The Result is {%s}", joiner.toString());
}
You won't have any extra comma at the end of the output.
CodePudding user response:
dellete last two Prints and add this* print in to seccond for loop :
System.out.println( numberB[k] (k<setofB ? "," : " }"));
CodePudding user response:
I have assumed that you are just a beginner and trying to learn that's why I try to use basic level solution.
public void himpunanIrisan(){
int[] numberB = {0,1,2,3,4};
int setofB = numberB.length-1;
int[] numberA = {0,1,2,5,6};
int setofA = numberA.length-1;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("The Result is {");
boolean initParam = true;
for(int k=1; k<=setofB; k ){
boolean Sama = false;
for(int i=1; i<=setofA; i ){
if(numberB[k] == numberA[i]){
Sama = true;
}
} if (Sama && initParam){
initParam = false;
stringBuilder.append(numberB[k]);
} else if(Sama) {
stringBuilder.append(",");
stringBuilder.append(numberB[k]);
}
}
stringBuilder.append("}");
System.out.println(stringBuilder);
}
}
The Result is {1,2}