0

enter image description here

here is the full code for LeeCode question 91;

I wanna run a test case in Java Visualizer but kept getting issues like that. Idk how to normally run an example like this to see how everything works.

public class ClassNameHere {
   public static void main(String[] args) {
      numDecodings("2622");
   }
   
   public int numDecodings(String s) {
        return s.length() == 0 ? 0 : numDecodings(0, s);
    }

    private int numDecodings(int p, String s) {
        int n = s.length();
        if (p == n) return 1;
        if (s.charAt(p) == '0') return 0;
        
        int res = numDecodings(p+1, s);

        if (p<n-1 && (s.charAt(p) == '1' || s.charAt(p) == '2' && s.charAt(p+1) < '7'))
            res += numDecodings(p+2, s);
        
        return res;
    }
}

I searched in Google and saw multiple anwsers firstly for how to executing code in Java Visulizer even checked the videos but understood nothing; then I searched specific quetsions showed in Java Visualizer. Both didn't work.

invzbl3
  • 5,872
  • 9
  • 36
  • 76
Gavin
  • 1
  • 1
  • It has nothing to do with Visualizer. It is a plain Java error. You are calling `numDecodings` (a non-static method) from `main` (a static method) and you cannot do that because `numDecodings` needs an _instance_ of your class. Try making `numDecodings` static. – k314159 Feb 06 '23 at 10:29

1 Answers1

0

You're trying to call numDecodings, which is a nonstatic method, from a static context (the main method). Since you don't need a nonstatic context for your numDecodings methods to work, you can just add a static to their definitions: public int numDecodings(String s) -> public static int numDecodings(String s)

0x150
  • 589
  • 2
  • 11