简体   繁体   English

如何修复 Java 中的错误源文件错误?

[英]how to fix a bad source file error in Java?

These are my codes:这些是我的代码:

public class Solution {

    public boolean isBalanced(String s) {
      Stack<Character> stack = new Stack<Character>();
        
        char c;
        for(int i = 0; i<s.length(); i++) {
            c = s.charAt(i);
            if(c == '[' || c == '(') {
                stack.push(c);
            }
            
            else if(c == ']' || c == ')'){
                if(stack.isEmpty()) {
                    return false;
                }
                
                else if(c == ']' && stack.peek() == '[') {
                    stack.pop();}
                else if(c == ')' && stack.peek() == '(') {
                    stack.pop();
                }
                else {
                    return false;
                }
            if (stack.isEmpty()){
                return true;
            }   
            }
            
            }
        
        return false;
  }

    
    public static void main(String[] args) {

        
        Solution solution = new Solution();
        System.out.println(solution.isBalanced("()")); // should be true
        System.out.println(solution.isBalanced("(")); // should be false
        System.out.println(solution.isBalanced("(])")); // should be false

    }

}

and I am getting this error:我收到了这个错误:

Solution.java:4: error: duplicate class: a22629.Solution
public class Solution {
^

MyTests.java:6: error: cannot access Solution
Solution solution;
^

bad source file: ./Solution.java
file does not contain class Solution
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors
Not compiled

This is the full error I am getting and I am not sure why.这是我得到的完整错误,我不知道为什么。

When I compile it in terminal it works, but when I want to submit it to my class portal, it does not work and gives me this error.当我在终端中编译它时它可以工作,但是当我想将它提交到我的 class 门户时,它不起作用并给我这个错误。

Please share more code Or add the command line data of your successful compilation and invocation.请分享更多代码或添加您成功编译和调用的命令行数据。

Possible Causes可能的原因

  1. You have default access scope for your class.您拥有 class 的default访问权限 scope。 Ensure that your class is public to be accessible to other's outside of your package.确保您的 class 是public的,以便 package 之外的其他人可以访问。 (Though locally you can still compile and run the class independently, other classes outside of your package can not access your class) (尽管在本地您仍然可以独立编译和运行 class,但 package 之外的其他类无法访问您的类)
  2. Check the package structure expected by the Test检查测试预期的 package 结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM