0

I was doing a coding challenge that prints a given text in a zig-zag:

thisisazigzag:

t     a     g
 h   s z   a
  i i   i z
   s     g

So I have my code (not sure if it's right or not yet, but that's not part of the question)

class Main { 

    public void zigzag(String text, int lines) {
        String zigLines = [];
        while(lines > 0){
            String line = "";
            increment = lines+(lines-2);
            lines = lines + (" " * (lines-1));
            for(int i=(lines-1); i<text.length(); i+=increment) {
                line = line + text[i] + (" " * increment);
            }
            zigLines.add(0, line);
            lines--;
        }
        for(line in zigLines){
            println(line);
        }
    }

    static void main(String[] args) {
        zigzag("thisisazigzag", 4);
    }
}

But when I run the script, I keep getting this error:

groovy.lang.MissingMethodException: No signature of method: static Main.zigzag() 
  is applicable for argument types: (String, Integer) values: [thisisazigzag, 4]
Possible solutions: zigzag(java.lang.String, int)

And I'm very confused as to the difference between java.lang.String and String and then Integer and int?

Any help or explanation of this would be great!

Thanks.

4
  • Did you try to make the method zigzag static? Commented Sep 14, 2018 at 21:15
  • I just did... and that worked..... Now I'm curious why does static make it work vs. fail miserably? Commented Sep 14, 2018 at 21:18
  • 1
    Without the static modifier zigzag was an instance method, meaning you would need an instance of your class Main to be able to call it. Here's an introductory tutorial explaining some of these concepts: docs.oracle.com/javase/tutorial/java/javaOO/classvars.html Commented Sep 14, 2018 at 21:28
  • 1
    @delephin can you post that as the answer, and I'll upvote it Commented Sep 14, 2018 at 21:29

1 Answer 1

4

You should make your zigzag method static.

Your code wasn't working because without the static modifier zigzag was an instance method, meaning you would need an instance of your class Main to be able to call it. Here's an introductory tutorial explaining some of these concepts: docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @delephin for this.

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.