Answer to Question #53290 in Java | JSP | JSF for francis

Question #53290
/everytime i run this program it shows my answer is incorrect. Please show me my problem import java.util.Scanner; public class Exercise04_22 { public static void main(String[] args) { Scanner input = new Scanner(; System.out.print("Enter string s1: "); String string1 =; System.out.print("Enter string s2: "); String string2 =; boolean retVal; retVal = string1.equals(string2); if (retVal = true) { // Returns -1 if not matched. System.out.println(string2 + " is a substring of " + string1); } else { System.out.println(string2 + " is not a substring of " + string1); } } }
Expert's answer
Pay your attention to this code line:
retVal = string1.equals(string2);
Method "equals" return true if and only if those strings are equal.
If you want to check if one string is a substring of another, you can do following:
1) Use method "contains". I'd prefer this way. String implements CharSequence interface, so there shouldn't be any problems.
2) If you are familiar with regular expressions, there is a method "matches".

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog