0

I am having difficulty triggering my "if" statement. When I input "I like my 'anything'", which the CharSequence should be searching for, the code kicks out the error from the else statement. I've tried to see if the contains method wasn't reading whitespace by attempting to identify just one letter using the CharSequence. That didn't help. I also attempted to change my contains method into a boolean and run the if statement if the boolean were true. That also did not work. I've searched around a bit at other code and it seems to look similar. Eclipse isn't flagging any errors I'm just beginning and have little clue on what else to attempt. If there are any additional hints on how to clean my code up or methods that might work better. Please give some constructive criticism.

import java.util.Scanner;

public class hello {

    public static void main(String args[]) {

        Scanner scan = new Scanner(System.in);
        try {
            System.out.println("Hi, what is your name?");
            String name1 = scan.next();
            System.out.println("Hello "+ name1 + ". Tell me what you like about yourself?\n"
                    + "Please pretext what you like about yourself with the phrase 'I like my'.");
            String selfEsteem = scan.next();
            CharSequence searchString = "I Like my";

            if (selfEsteem.contains(searchString)) {
                selfEsteem = selfEsteem.replace("my", "your");
                System.out.println(selfEsteem + "also.");
            } else {
                System.err.println("Error: User did not use 'I like my' input format");
            }
        } finally {
            scan.close();
        }
    }

}

output:

Hi, what is your name? Janet Hello Janet. Tell me what you like about yourself? Please pretext what you like about yourself with the phrase 'I like my'. I like my boobs Error: User did not use 'I like my' input format

Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
ucodebro3
  • 1
  • 1
  • 1
  • 1
  • 2
    Check the casing on the word Like, perhaps? Contains should be case-sensitive and you're looking for "I Like my", not "I like my" – daf Jul 02 '16 at 11:25
  • I converted the "L" to a "l" , saved the file, and meticulously type my answers only to arrive at the same error message? Any other ideas? – ucodebro3 Jul 04 '16 at 14:21
  • My mistake was in the scan.next() method instead of using scan.nextLine(). Thanks for your time. – ucodebro3 Jul 07 '16 at 02:49

3 Answers3

0

Your searchString is "I Like my", while your text has "I like my". So I assume your user will enter the lowercase one and contains is case sensitive so it won't find it.

Change to:

CharSequence searchString = "I like my";
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • I have made the proposed changes and still receive the error message. I also tried declaring the searchString as type String instead of type CharSequence and was wrong. – ucodebro3 Jul 04 '16 at 14:41
0

You are searching for "I Like my" while your error massages says it should be 'I like my'.

It's case sensitive.

Either type in "I Like my" or change your searchString to "I like my".

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • I have changed my searchString to "I like my" and am met with the same error message from the else statement. Any additional ideas? – ucodebro3 Jul 04 '16 at 14:43
  • Can you copy the exact input you're typing in? – Shiro Jul 04 '16 at 14:46
  • I found my mistake. I was only using scan.next() instead of scan.nextLine() which was throwing everything wacky. Thanks for your time though. – ucodebro3 Jul 07 '16 at 02:48
0

Use

if (selfEsteem.containsIgnoreCase(searchString))
    ...

instead of

if (selfEsteem.contains(searchString))
    ...

Then it doesn't matter what case the user types in.

Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21