• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring errors are driving me crazy  RSS feed

 
tam mann
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't get this search thing to work. I got it so it doesn't give the the OutOfBounds error any more, but it just doesn't do the job either. From what I can tell, count is not going up.

***************************
public class lab712 {
public static void main (String[] args) {
String word;
System.out.println("What's the Word?");
word = JJS.inputString();
String pattern;
System.out.println("What letter pattern do you want me to find?");
pattern = JJS.inputString();
findPattern(word, pattern);
}//end main

public static void findPattern(String s, String p) {
int slength = s.length();
int plength = p.length();
int count = 0;
int start = 0;
int end = plength;
while ((start < slength - 1) && (end <= slength - 1)){
if (s.substring(start,end) == p) {
count++;
}//end if
end++;
start++;
}//end while
if (count == 0) {
System.out.println("No matches");
} else {
System.out.println("That pattern occurs: " + count);
}//end if else
}//end method

}//end class
*********************************
Any ideas?

Thanks
 
Padma Lalwani
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you should use "equals" instead of == for String comparison
Thanks
Padma
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it will help a lot if you use UBB CODE tags to preserve the formatting of your code. There are buttons just below the message editing box to help you.

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!