最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - I don't understand the compilation error I'm getting from CodeForces - Stack Overflow

programmeradmin0浏览0评论
import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        int trials = input.nextInt();
        input.nextLine();

        for(int i = 0; i < trials; i++) {
            String userInput = input.nextLine();
            System.out.println(countNumberOfOnes(userInput));
        }

        input.close();

    }

    public static int countNumberOfOnes(String input) {
        int numberOfOnes = 0;

        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) == '1') {
                numberOfOnes++;
            }
        }

        return numberOfOnes;

    }
}

Explanation of the code: the user enters the number of times he wants the code to work "trials". He enters strings of 0 and 1's and my code counts the number of 1's in each string in each trial.

The code runs perfectly on my IDE's compiler but when I try to submit the code by copying it from my IDE I get this error:

Can't compile file:
Main.java:31: error: reached end of file while parsing
    }
     ^
1 error
File should contain public class named as the file. 

I tried uploading the file instead of copying, but I still got a compilation error, just a different one:

Can't compile file:
2062A.java:3: error: <identifier> expected
public class 2062A-String {
           ^
2062A.java:31: error: reached end of file while parsing
   }
    ^
2 errors
File should contain public class named as the file.  

Update: my code worked with the other java compiler!

发布评论

评论列表(0)

  1. 暂无评论