Loading...

Java Mini Project

Build one small terminal program that asks what is in a box, asks how many, and prints one label line. In Box.java, keep the public class Box, import java.util.Scanner, ask with item: and how many:, store the answers, and print label(item, count). Compile with javac Box.java and run java Box. Type mug, press Enter, type 4, press Enter. The last line on the screen should be mug 4. Leave Program Arguments for the next page. This lesson is a short conversation you run yourself, not a fresh compiling theory page and not a Summary map.

Ask twice, then print one label

Open Box.java. Keep the public class name Box. Delete leftover Build compiling demos, leftover Recap summary lines, leftover Dice Random rolls, and leftover Stamp date prints if they bury the mini-project story. This file must wait for typing on purpose.
Add the import at the top:
import java.util.Scanner;
Keep a small helper that joins the two answers with one space:
public static String label(String item, String count) {
return item + " " + count;
}
Inside Box.main, build a scanner, ask twice, then print the label:
Scanner in = new Scanner(System.in);
System.out.print("item: ");
String item = in.nextLine();
System.out.print("how many: ");
String count = in.nextLine();
System.out.println(label(item, count));
You will type mug, then 4. The last line will be mug 4. The prompts stay on the waiting lines. They are not stored inside the label.

Box asks twice then prints mug 4

java
import java.util.Scanner;

public class Box {
    public static String label(String item, String count) {
        return item + " " + count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("item: ");
        String item = in.nextLine();
        System.out.print("how many: ");
        String count = in.nextLine();
        System.out.println(label(item, count));
    }
}

From the folder that holds Box.java, run javac Box.java. When braces and semicolons match, javac writes no line of its own and writes Box.class. Then run java Box — not java Box.java. Keep one public class so the name matches Box.
The screen shows a short conversation. The program waits at item: and you type mug, then press Enter. item holds mug. The question text item: is only shown while the program waits, so it is not stored in item. The program then waits at how many: and you type 4, then press Enter. count holds 4, the characters you typed. The question how many: is not stored either. println writes the line label returns. That line is mug 4. The space between mug and 4 sits in the return expression. The program is done. There is no fourth result line after the label.
Say the beginner rule while you watch the run: two prompts, two answers, one joined label. Do not invent Program Arguments demos on this page. Program Arguments waits for the next lesson. Do not treat item: mug as the label. Do not treat how many: 4 as the label.
If you still see ok or ok then built from a leftover Build compiling file, switch to Box.java, compile, and run java Box with the two answers. If javac cannot find Scanner, check the import line at the top of Box.java.

The last line is the label, not the questions

The last line is mug 4. It is not item: mug. It is not how many: 4. Those prompts stay on the waiting lines beside what you typed. label receives only mug and 4. If the return line said item + ": " + count, the last line would be mug: 4, but that is not the line in this program. This return line puts one space between the answers, so the last line is mug 4.
count holds the characters 4 as a String from nextLine. You are not parsing an int on this page. You are not rolling Random. You are not printing ready 42 from Recap. You are labeling one box with two typed answers.
If you type different words, the label changes. The teaching quiz and the walkthrough keep mug and 4 so the last line stays predictable. Practice once with those answers before you experiment.

Mini Project is not Compiling and not Program Arguments

Compiling taught javac writing .class and java loading it with Build. Mini Project uses those habits and adds a real conversation with Scanner. Program Arguments will show how main's args array works when you start the program with extra words on the next page. Do not mix those three jobs in one beginner file today.
Keep System.out.print for the prompts so the answer stays on the same visual line as the question in many terminals. Keep println for the final label so it ends on its own line. Keep nextLine so a whole answer is read after Enter.
Without the import, javac fails before any prompt appears. Without typing the two answers, the program waits. Without label, you could still join in println, but this page keeps a named helper so methods stay visible in the mini project.

Walk the typed run once more

Start at Box.main. The scanner reads from System.in. The first print shows item:. You type mug and press Enter. nextLine stores mug in item. The second print shows how many:. You type 4 and press Enter. nextLine stores 4 in count. label(item, count) returns mug 4. println writes that string. The run ends.
If the last line shows mug4 with no space, check that the return expression still includes " ". If the last line shows item: mug 4, you accidentally included the prompt text in the join — keep prompts only in the print calls. If nothing waits, you may still be running a Build file that prints fixed words with no Scanner.
Leftover Build and Recap files may stay nearby as history. For this checkpoint run the Box teaching file until the last line shows mug 4 after you type those two answers.

Folder and compile habits still matter

List the folder after a successful compile. You should see Box.java and Box.class together. Edit the source, save, run javac Box.java, then run java Box again. A stale .class can show an old label shape if you skip compile after an edit — the same Compiling lesson, now attached to a Scanner program.
Keep one public class named Box. Do not paste Program Arguments loops into this file yet. Do not add Random seeds to “enrich” the label. Do not freeze a LocalDate inside Box. The mini project is intentionally small: two questions, one label.
When javac complains about duplicate public classes, keep only Box public. When the run cannot find or load Box, confirm you are in the folder that holds Box.class and that the compile succeeded.

Before Program Arguments

You can keep public class Box, import Scanner, ask item: and how many:, store the answers, print label(item, count), compile with javac Box.java, run java Box, type mug then 4, and see mug 4 as the last line. The prompts are not part of that label. The next page is Program Arguments, where you pass words on the command line when you start the program.
Keep the Box conversation for a clear mini project. Keep Build compiling demos off this page when you practice the Scanner label. Keep Recap Summary files off this page when you practice the label. Keep Program Arguments demos off this page until that lesson. Those habits are the checkpoint before you move on. When the last line shows mug 4 after the two answers, Mini Project did its job for this lesson and you are ready for Program Arguments.
One last pass through the folder: one public Box, import present, two print prompts, two nextLine stores, one label join, no Random, no LocalDate, compile then run. Run javac Box.java and java Box once more. Type mug, then 4. If the last line still shows mug 4, the Mini Project shape is solid and the course can move to Program Arguments on the next page. Prefer one clear conversation habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top