Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

error on output

+3 votes
asked Dec 21, 2021 by Dery Anjaya (150 points)

Hello, can someone help me please ? 

i'm newbie in programming, in this is the first time i use onlinegdb

i have test the simple code on java, if I compile in PC it works, 

but if I run in onlinegdb, the output says : 

Main.java:11: error: class Latihan is public, should be declared in a file named Latihan.java
public class Latihan {
1 error.
and the code as below :

import java.util.Scanner;

public class Latihan {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int totalMahasiswa = 0;
        int totalNilai = 0;
        int nilai = 0;
        int rataRata = 0;

        System.out.print("Masukan jumlah mahasiswa:");
        totalMahasiswa = scanner.nextInt();

        for(int i = 1; i <= totalMahasiswa; i++) {
            System.out.print("Masukan nilai mahasiswa ke " + i + ":");
            nilai = scanner.nextInt();
            totalNilai = totalNilai + nilai;

        rataRata = (totalNilai/totalMahasiswa);

        System.out.println("Nilai rata-rata :" + rataRata);

please help, Thank you very much


2 Answers

+1 vote
answered Dec 27, 2021 by Peter Minarik (55,760 points)
edited Dec 28, 2021 by Peter Minarik
In Java, the class name and the file name must match. Your file is probably called Main.java, while your class is called Latihan.

You can rename your class to be Main or move your Latihan class to Latihan.java and call it from the Main.java file.
commented Dec 28, 2021 by Dery Anjaya (150 points)
Thank you Very Much Peter
commented Dec 28, 2021 by Peter Minarik (55,760 points)
My pleasure.
0 votes
answered Dec 28, 2021 by Sourav Suvarna (150 points)

Just change  public class Latihan  to public class Main

commented Dec 28, 2021 by Dery Anjaya (150 points)
Thank you very much Sourav
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.