0

my goal here to remove the word "like" if it appears at the end of the users input which gets replicated as a string. But I am struggling to see what the problem is with my work.

When this is ran, anything before the word "like" is removed and I can't understand why so any advice would be appreciated, thanks in advance.

System.out.println("Type in an input, plez?");
String userInput4 = inputScanner.nextLine();
int userInput4Length = userInput4.length();
if (userInput4.toLowerCase().endsWith("like")) {
    String partOfString3 = userInput4.substring(userInput4Length- 4);
    System.out.println("There is a 'like' in your input, let me remove that for you:- " +partOfString3);
} else {
    System.out.println(userInput4);
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
lg99
  • 71
  • 3
  • 11
  • It doesn't look like you ever use code to get before the "like", `userInput4` never changes, and `partOfString3` is the "like" itself. What is supposed to contain the output/result? – phflack Nov 17 '17 at 19:39
  • The parameter to `substring()` indicates the starting position - the posted code starts near the end of the original string. Try using the 2 parameter version of the method. – Andrew S Nov 17 '17 at 19:41

1 Answers1

0

substring() method's first parameter is beginIndex. So, you should write

String partOfString3 = userInput4.substring(0, userInput4Length- 4);

instead of

String partOfString3 = userInput4.substring(userInput4Length- 4);
Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42