about | help | code help+videos | done | prefs |
public boolean endsGerund(String str)
public boolean endsGerund(String str) { boolean ends= false; if (_____) { ends = true; } return ends; }Use the String method endsWith() inside the parentheses: public boolean endsGerund(String str) { boolean ends = false; if ( str.endsWith("ing") ) { ends = true; } return ends; }Note that - unlike startsWith() - there are NOT two versions of endsWith(). That is, there is no version with a 2nd parameter. That means that any string you test will only return true if it actually ENDS WITH the string you are testing for. ______________________________________ NOTE: There is actually a way to solve this problem using startsWith(). public boolean endsGerund(String str) { boolean ends = false; int len = str.length(); if ( str.startsWith("ing",len-3) ) { ends = true; } return ends; }Why do we use "len-3" for the 2nd parameter? Because the length of "ing" is 3. This causes startsWith() to start checking beginning at the 3rd-to-last character. If we wanted to check whether a String ends with "ment", we would use len-4: str.startsWith("ment",len-4) This causes startsWith() to start checking beginning at the 4th-to-last character. If we wanted to check whether a String ends with "ed", we would use len-2: str.startsWith("ed",len-2) This causes startsWith() to start checking beginning at the 2nd-to-last character. endsGerund("walking") → true endsGerund("said") → false endsGerund("ate") → false ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 1
Copyright Nick Parlante 2017 - privacy