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
The problem is that you declare
public static void main(String[] args)in the innerSolutionclass. It was not allowed before Java 16. The possible solutions:Solutionclass will be a top-level class and theResultis an inner class or even just separated top-level class as well.