Create a Java Coding Challenge
Build and test a Java coding challenge in LeetLabs, with a complete example.
This guide shows how to build a Java coding challenge in LeetLabs, with a complete example that we built and tested on Learnyst. Use it as a template for your own Java exercises. Read Create a coding challenge first for the general steps.
On this page
- How input and output work in Java
- Example: Sum of two numbers
- Test it in Preview
- Tips for Java challenges
- Practice ideas for Java
How input and output work in Java
Your program reads the test case Input from standard input with a Scanner and prints its answer with System.out.println. Put the code in a public class Main, as in the starter snippet.
The program's printed output must match the test case Output exactly, including spaces and line breaks.
Example: Sum of two numbers
Create a challenge with Language set to Java, then fill in each step.
Problem Statement:
Sum of Two Numbers
Read two integers a and b from a single line of input, separated by a space. Print their sum.
Example input: 2 3
Example output: 5
Constraints: -1000 <= a, b <= 1000
Initial Snippet (the starter code learners see):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
// Write your code below
}
}
Test Cases:
| Title | Input | Output | Visibility |
|---|---|---|---|
| Positive numbers | 2 3 |
5 |
Visible |
| Negative numbers | -4 10 |
6 |
Hidden |
Hints: one to explain the input is already read, and one to show how to print the result.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
}
Advanced Options: Difficulty Easy, Unlock Hints After 1 code run.
Test it in Preview
Click Preview, then TAKE CHALLENGE. Learners see the problem on the left, your starter code in the editor, and the visible test case below.

Code that subtracts instead of adding prints -1 for the input 2 3, so RUN shows Wrong Answer.

The correct solution prints 5 and shows Correct Answer. The hidden test case is checked when the learner submits.

Tips for Java challenges
- Keep the class name
Mainin the starter snippet and the solution. - Use
sc.nextInt(),sc.nextLong()orsc.nextLine()to read values. - Use
System.out.printlnfor each line of output.
Practice ideas for Java
- Check whether a string is a palindrome
- Find the maximum in an array
- Count characters with a HashMap
- Reverse words in a sentence
- Implement a simple class and print its fields
What to do next
- Create a coding challenge: all the steps and options.
- Publish and unpublish code challenges: make the challenge available for your courses.
- Embed a coding challenge into courses: add it as a lesson.
If you have questions, contact us at support@learnyst.com.