The snippet library.
Small, practical examples. Search by task or narrow things down below.
Name a value that stays fixed
Java: Use a constant declaration or the language’s constant naming convention for a fixed limit.
Java✓ Tested
Print a number with two decimal places
Java: Format a small decimal value consistently for display.
Java✓ Tested
Read one character from ASCII text
Java: Use a valid zero-based index to read the first character.
Java✓ Tested
Read the beginning of a text value
Java: Extract or display the first three ASCII characters from a known nonempty string.
Java✓ Tested
Read the first collection element
Java: Access index zero only after establishing that the collection is nonempty.
Java✓ Tested
Repeat a fixed number of times
Java: Use a counted loop to print three numbered steps.
Java✓ Tested
Repeat while a condition holds
Java: Count down while a positive number remains, updating the loop variable on each iteration.
Java✓ Tested
Replace one collection element
Java: Update an existing element at a known valid index.
Java✓ Tested
Require two conditions to be true
Java: Combine a lower and an upper bound to check whether a value is in range.
Java✓ Tested
Run your first program
Java: Start with a complete program that prints one line. Follow the run instructions below.
Java✓ Tested
Store a value in a named variable
Java: Give a value a descriptive name and print it.
Java✓ Tested
Update a running count
Java: Change a variable after its initial value has been assigned.
Java✓ Tested