Reputation: 49
I having a hard time getting the right output because i dont know how boolean exactly works inside a method. I have an arraylist and I'm check for any duplicates in the arraylist Here's my code
public void rentOneInstrument(List<Instrument> instrumentList){
String a="";
String b="";
boolean found = false;
for (int j = 0; j < instrumentList.size(); j++) {
a ="";
a = instrumentList.get(j).getName();
for (int i = j+1; i < instrumentList.size(); i++) {
b = instrumentList.get(i).getName();
System.out.println("a" + a + " b" + b);
if(a.equals(b)){
found = true;
}else {
found = false;
}
}
}
if (found) {
System.out.println("duplicate");
}else{
System.out.println("no duplicate");
}
}
Here is my output
a Cymbals b Drums,..
a Cymbals b Cello,..
a Cymbals b Cymbals,..
a Drums b Cello,..
a Drums b Cymbals,..
a Cello b Cymbals
no duplicate // prints no duplicate when there is clearly a duplicate in the output. How can i correct this?
Edit.. Btw I just a want a single out put that prints whether it found a duplicate or not inside the loop
Upvotes: 0
Views: 189
Reputation: 409
public void rentOneInstrument(List<Instrument> instrumentList){
boolean found=false;
List<String> listString=new ArrayList<String>();
for (Instrument inst: instrumentList){
if(listString.contains(inst.getName())){
found=true;
}
else{
listString.add(inst.getName());
}
}
if (found) {
System.out.println("duplicate");
}else{
System.out.println("no duplicate");
}
}
Upvotes: 0
Reputation: 6816
Consider the following input :
a,b,c,a,d
Now whit your code it would be false as d is not repeated. It would over right the value for a that is repeated. Also once a element is fond that it is replanted you don't need to go through the entire loop of all elements hence there is a break.
public void rentOneInstrument(List<Instrument> instrumentList){
String a="";
String b="";
boolean found = false;
for (int j = 0; j < instrumentList.size(); j++) {
a ="";
a = instrumentList.get(j).getName();
if(found) { break; } // Changed line here
for (int i = j+1; i < instrumentList.size(); i++) {
b = instrumentList.get(i).getName();
System.out.println("temp1 " + a + " temp2 " + b);
if(a.equals(b)){
found = true;
break; // Changed line here
}
}
}
if (found) {
System.out.println("duplicate");
}else{
System.out.println("no duplicate");
}
}
Another way of doing it without a break would be
public void rentOneInstrument(List<Instrument> instrumentList){
String a="";
String b="";
boolean found = false;
for (int j = 0; j < instrumentList.size() && !found; j++) {
a ="";
a = instrumentList.get(j).getName();
for (int i = j+1; i < instrumentList.size() && !found; i++) { // Changed for condition to look at the found variable too.
b = instrumentList.get(i).getName();
System.out.println("temp1 " + a + " temp2 " + b);
if(a.equals(b)){
found = true;
}
}
}
if (found) {
System.out.println("duplicate");
}else{
System.out.println("no duplicate");
}
}
A much better way of doing this would be using sets that doesn't contain duplicates.
public void rentOneInstrument(List<Instrument> instrumentList){
Set< Instrument > instrumentSet = new HashSet< Instrument >(instrumentList);
if(instrumentList.size()== instrumentSet.size()) {
System.out.println("no duplicate");
} else {
System.out.println("duplicate");
}
}
Upvotes: 1
Reputation: 44740
Let found
be false
by default. and just set it true
if any duplicate is found.
if(a.equals(b)){
found = true;
}else {
// found = false;
// don't do this otherwise it will override previous `found` value
}
Upvotes: 1
Reputation: 1679
if(a.equals(b)){
found = true
}else {
found = false;
}
This is your problem. This way only the last iteration of your loop will be stored in found
. Since you are initializing it as false
you don't need to set it to that again here.
for (int i = j+1; i < instrumentList.size(); i++) {
b = instrumentList.get(i).getName();
System.out.println("temp1 " + a + " temp2 " + b);
if(a.equals(b)){
found = true;
}
}
Alternatively, you can use a break
statement when you have found a match to get out of the loop like so:
if(a.equals(b)){
found = true;
break;
}else {
found = false;
}
This way, found
will be true and no other iterations will be performed, continuing after the end of the loop instead.
Upvotes: 2