0

I know this has question has been asked countless times.. but I can't seem to get it to work. the problems are,

  1. Even converting everything to lower case doesn't work out when "computer" and "Comp" are entered.
  2. If the string is a sentence (I add a space), it essentially skips the substring code and says "Not in the string"..

Help is appreciated. thanks!

Scanner in=new Scanner(System.in);
System.out.println("\fEnter the main string:");

String GivenString=in.next();
System.out.println("Enter the substring :");

String SubString=in.next();

GivenString.toLowerCase();
SubString.toLowerCase();

if(GivenString.indexOf(SubString)!=-1)
{
    System.out.println("Substring is in the given string.");
}
else
{
    System.out.println("Substring is not in the given string.");
}
2
  • 2
    Please use proper naming conventions. It makes code easier to read. Commented Apr 20, 2013 at 19:55
  • Thanks @syb0rg. Changing them right away. Commented Apr 20, 2013 at 19:57

1 Answer 1

7

Strings are immutable and toLowerCase() returns a new String object. These lines:

GivenString.toLowerCase();
SubString.toLowerCase();

...do not modify the values in GivenString and SubString.

You would need to modify them to:

GivenString = GivenString.toLowerCase();
SubString = SubString.toLowerCase();
Sign up to request clarification or add additional context in comments.

1 Comment

Also, note that by convention in Java (unlike some other languages, like C#) variable names start with a lower case letter, which distinguishes them from class names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.