Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Leen Leenaerts
Leen Leenaerts
1,096 Points

What is wrong with my code?

<p>

public String getSandwich(String str) {



  // remove first bread
  int i = 0;
  String bread1Removed = "";
  while (i<(str.length()-9) && bread1Removed == "") {

    if (str.substring(i,i+5).equals("bread")){
    bread1Removed = str.substring(i+5);
    }

  i++;

  }


  // remove second bread
  i = bread1Removed.length()-5;
  String bread2Removed = "";


 while (i>0 && bread2Removed == "") {

    if (str.substring(i,i+5).equals("bread")){
    bread2Removed = bread1Removed.substring(0,i);
    }

  i--;

  }


return bread2Removed;
}
</p>
Leen Leenaerts
Leen Leenaerts
1,096 Points

exercise from http://codingbat.com/prob/p129952

if i run it i get the wrong outcome works for breadbreadbreadbread but not for breadjambread.

The problem should be in // remove second bread

1 Answer

Leen Leenaerts
Leen Leenaerts
1,096 Points

Found it finally, just forgot to change the string in the if statement, how can i be this stupid :o

Michael Hess
Michael Hess
24,512 Points

It happens to all of us. Keep up the good work!

thats being a programmer