Training Site
Sideways banner

Java

Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 100 megabytes
Time limit: 1.0 seconds

I/O, Code, and Documentation

  • Your file must contain a class which must be called Main.
  • Your class must contain a method called main.
  • Get input from System.in.
  • Print to System.out.
  • You are permitted to view the Java SE 11 API Specification during an NZIC contest.

Get OpenJDK.

Learn how to run a hello world program.

Example

Adding two numbers. Numbers will be space separated on a single line.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(sc.nextInt() + sc.nextInt());
    }
}


Note

On problems with larger inputs, Scanner may be too slow. In such cases, we recommend using BufferedReader and StringTokenizer instead. For example:

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        System.out.println(Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken()));
    }
}


Submit

Sample Explanation

3 + 4 = 7

  • Sample Input 1

    3 4
    

    Sample Output 1

    7