I need some pointers as to how to write the main method for this mini-search engine I'm writing. Here is my code:
public class StringSearch {
private String s1 = "ACTGACGCAG";
private String s2 = "TCACAACGGG";
private String s3 = "GAGTCCAGTT";
public static void main(String[] args) {
System.out.println("Welcome! The strings you started with are:\n" + s1 + "\n" + s2 + "\n" + s3);
}
public void search() {
do {
for(int i = 0; i < s1.length() - 4; i++) {
int d = 0;
String subStr = s1.substring(0 + i, 4 + i);
do{
for (int iSub = 0; iSub < 4; i++){
if (subStr.charAt(iSub) != (subStr.charAt(iSub))) {
d += 1;
}
}
}while(d < 2);
if(s2.contains(subStr) && s3.contains(subStr)) {
System.out.println(subStr + "is in all 3 lists.");
}
}
}while (s1.length() - 4 < 6);
System.out.println("Task Complete.");
}
}
The idea is that I've got a set of 3 strings to start with and I need to create a substring of 4 characters and compare it to all 3 strings to see if it is included in each with at least 3/4 letters matching. For example, if I take the first 4 characters of s1 (ACTG), then 'CCTG', 'ACAG', 'ACTA', 'AATG' would all be valid search results and would be returned.
The problem I'm having is at the main method. How exactly am I supposed to instantiate the search method syntactically? I tried StringSearch s1 = new StringSearch(); and then s1.search(); but didn't get any results. Also when I try to reference the original strings in the println it says I can't reference them from a static context. Java novice here, specific help would be greatly appreciated.
new StringSearch().search()
is the correct thing to do in your main method; the reason it doesn't work is that your actualsearch
method likely has problems. Read the link.