0

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

Onedaynerd
  • 49
  • 2
  • 9
  • See [Prevent duplicate entries in arraylist](http://stackoverflow.com/questions/14192532/how-to-prevent-the-adding-of-duplicate-objects-to-an-arraylist): consider using a `set` – fantaghirocco May 19 '15 at 15:15

4 Answers4

2
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.

Dragondraikk
  • 1,659
  • 11
  • 21
1

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");
     }
}
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • Do you really think that just putting out code without any explanation whatsoever is wrong will help to understand what is going on? – GhostCat May 19 '15 at 15:13
  • hi. if i insert it inside the for- loop if prints after every ouput. What i want to do is to print a single output if there is a duplicate in the entire list – Onedaynerd May 19 '15 at 15:14
  • @Onedaynerd Please check the code and understand what has changed. It should not be change you printing everytime. – StackFlowed May 19 '15 at 15:17
  • @Onedaynerd check the suggested solution. – StackFlowed May 19 '15 at 15:28
1

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
 }
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
0
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");
        }


}
tipiwiny
  • 399
  • 1
  • 3
  • 18