( modifier 'static' is only allowed in constant variable declarations ) compilation error

51 Views Asked by At
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {
    /*
     * Complete the 'isBalanced' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */

    public static String isBalanced(String s) {
    int getLength=s.length()-1;
    int temp=0;
    while(temp<s.length()/2){
        if(s.charAt(0)==s.charAt(getLength)){
            temp++;
            getLength--;   
            }
        else{
            return "NO";
            }
        }
        return "YES";
    }   

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, t).forEach(tItr -> {
            try {
                String s = bufferedReader.readLine();

                String result = Result.isBalanced(s);

                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
      }
    }
}

Whenever solving a question in HackerRank and there is static declaration in the function it is giving me

( Solution.java:37: error: Illegal static declaration in inner class Result.Solution public static void main(String[] args) throws Exception { ^ modifier 'static' is only allowed in constant variable declarations compilation ) error................

I don't have much idea since i'm a begginer

2

There are 2 best solutions below

0
Andrej Istomin On

The problem is that you declare public static void main(String[] args) in the inner Solution class. It was not allowed before Java 16. The possible solutions:

  1. Use Java 16+
  2. Restructure your code, so this Solution class will be a top-level class and the Result is an inner class or even just separated top-level class as well.
0
Elliott Frisch On

Three problems. You need to close the class Result before you can start Solution. You should be using if (s.charAt(temp) == s.charAt(getLength)) { instead of if (s.charAt(0) == s.charAt(getLength)) { and then you have an extra closing brace after Solution.

class Result {
    public static String isBalanced(String s) {
        int getLength = s.length() - 1;
        int temp = 0;
        while (temp < s.length() / 2) {
            if (s.charAt(temp) == s.charAt(getLength)) {
                temp++;
                getLength--;
            } else {
                return "NO";
            }
        }
        return "YES";
    }
}

Related Questions in ILLEGALSTATEEXCEPTION