Skip to main content

Online Java Compiler

Main.java
Ready

About Java

Java is a typed, object-oriented language used for Android apps, back-end services and most college DSA courses in India. Every program starts from a class with a main method.

How to take input in Java

Create one Scanner on System.in and read with nextInt(), nextLine() or next(). After nextInt(), call nextLine() once before reading a full line, or you get an empty string.

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();          // a whole number
double d = sc.nextDouble();    // a decimal
sc.nextLine();                 // finish the current line
String line = sc.nextLine();   // a full line of text

Java programs to try

Copy one into the editor above, put any input in the Input tab and press Run.

Reverse a number

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        int rev = 0;
        while (n > 0) { rev = rev * 10 + n % 10; n /= 10; }
        System.out.println(rev);
    }
}

Input: 12345

Check if a number is prime

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();
        boolean prime = n > 1;
        for (int i = 2; (long) i * i <= n; i++) if (n % i == 0) { prime = false; break; }
        System.out.println(prime ? "Prime" : "Not prime");
    }
}

Input: 97

Sort an array

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
    }
}

Input: 5 ⏎ 42 7 19 3 25

Common Java errors and how to fix them

error: class X is public, should be declared in a file named X.java
The public class must be called Main here, because the file is Main.java. Rename your class to Main.
java.util.NoSuchElementException
Scanner ran out of input. Add every value your program reads to the Input tab.
java.util.InputMismatchException
nextInt() found text that isn't a whole number. Check the order and type of your input values.
error: ';' expected
A statement is missing its semicolon. The line number in the message points at (or just after) the problem.

Frequently asked questions

Is this online Java compiler free?

Yes. Write and run Java code in your browser for free, with nothing to install and no setup.

How do I give input to my Java program?

Type it in the Input tab before pressing Run, and read it with a Scanner on System.in. Put values on separate lines or separate them with spaces; nextInt() skips spaces and line breaks.

What should my class be called?

Use public class Main with a public static void main(String[] args) method, since the file is Main.java.

Does it work on my phone?

Yes. The editor, input and output work in a mobile browser, so you can practise Java without a laptop.