0

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.

5
  • 3
    Recommended reading: How to debug small programs
    – durron597
    Aug 20, 2015 at 3:26
  • In particular, new StringSearch().search() is the correct thing to do in your main method; the reason it doesn't work is that your actual search method likely has problems. Read the link.
    – durron597
    Aug 20, 2015 at 3:28
  • Do not add links because these can get offline any day. Others will not be able to benefit from it in the future. Aug 20, 2015 at 4:01
  • 1
    @GoyalVicky That rule is only for answers, this is a comment and it should be expected they can be deleted at any time. Besides, that's Eric Lippert's blog, I expect it will be around for some time.
    – durron597
    Aug 20, 2015 at 4:12
  • @durron597 Would't it be better if instead of providing a solution to a third party link in the comment if you can provide the main points of blog along with yours as a part of solution with a reference to that blog so that even if it goes offline we have the solution so that in future others can benefit from it as well. I mean its entirely up to you. Aug 20, 2015 at 4:37

1 Answer 1

0

You cannot refer fields directly in static methods. First you need to create an object of it like this:-

public static void main(String[] args) {  
    StringSearch stringSearch = new StringSearch();
    stringSearch.search();
    System.out.println("Welcome!  The strings you started with are:\n" + stringSearch.s1 + "\n" + stringSearch.s2 + "\n" + stringSearch.s3);
}

For issues with the search method you need to debug your method.

Steps:-

  1. If you are using eclipse, double click on the line number on the left or Right click and select Toggle breakpoint for where you want your main thread to halt.

  2. Right click on program and use Debug as option.

  3. Use F5, F6, F7 or F8 keys to debug

F5 executes the currently selected line and goes to the next line in your program. If the selected line is a method call the debugger steps into the associated code.

F6 steps over the call, i.e. it executes a method without stepping into it in the debugger.

F7 steps out to the caller of the currently executed method. This finishes the execution of the current method and returns to the caller of this method.

F8 tells the Eclipse debugger to resume the execution of the program code until is reaches the next breakpoint or watchpoint.

1
  • Earlier his code is not compiling but now it will. He needs to debug his search method and correct his code based on it. Aug 20, 2015 at 4:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.