Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,511 Points

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

thats being a programmer