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!