Loading...

Java Input

You want the screen to show a word you type after the program has started, a word that is not written in the file. On the last page the word sat between quotes on the storing line, so a different screen meant editing Mint.java. On this page the file does not contain leaf at all. It creates a name, plant, and then waits. You type leaf and press Enter. The writing line uses the bare name, and the program writes leaf. You are not installing Java again. You are not adding a note, and you are not learning a second way to end a line. You already know a name can hold letters and that println writes those letters. The new work is filling that name from the line you type.

The file waits for one typed line

Open Mint.java. The file name stays Mint.java, because public class Mint is still the name of the file. This page does not rename the file, and it does not rename the class. The first line of the file, above public class Mint, is import java.util.Scanner;. That line lets the file use Scanner. It is not a writing line. Scanner is not a letter for the screen. The semicolon ends the import line. Leave a blank line under it, then the class, the same public class Mint you already have. This file still has no package line. The import is the first line, not a line inside main.

Inside main, the first new line makes a reader: Scanner reader = new Scanner(System.in);. Scanner says what kind of thing reader is. reader is the name, and it starts with a small r. The = sign stores the reader in that name. new Scanner(System.in) makes the reader and points it at System.in. System.in is the place typed letters come from. System.out, which you already use on the writing line, is the screen. Those two are not the same. The semicolon ends this line. This line does not write the screen, and it does not wait for you to type. It only makes the reader.

The next line fills the name you already understand: String plant = reader.nextLine();. String still means this name holds letters. plant still starts with a small p. The = sign still stores letters in that name. What is new is the right side. reader.nextLine() does not have quotes. There is no leaf on this line. The line asks the reader for one typed line. The program waits there until you press Enter. When you press Enter, nextLine stores the letters you typed before that Enter. It does not store the Enter itself. Then the line ends at the semicolon. Storing is still not writing. The program has not written its own line yet.

The writing line stays the bare name, the same shape as the file you kept on the last page: System.out.println(plant);. No quotes. Java reads the name plant, writes the letters that name is holding, and ends the line. Those letters are whatever nextLine just stored. If you typed leaf, the program writes leaf. If you typed stem, the program writes stem. The file did not change between those two runs. Save the file.

The file you keep

Save this as Mint.java. It is the file this page keeps. The word you will type is not in the file. Search the file for the letters l, e, a, f. They are not there. The reader is named reader. The stored line is named plant. The writing line uses plant and does not contain quotes.

the file you keep

java
import java.util.Scanner;

public class Mint {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String plant = reader.nextLine();
        System.out.println(plant);
    }
}

Read the file from the top and decide what can reach the screen. import java.util.Scanner; only lets the file use Scanner. public class Mint names the file. public static void main(String[] args) is where Java starts. None of those words are letters for the screen. The word String on the main line is still not the String that creates plant. Scanner reader = new Scanner(System.in); makes the reader. It writes nothing. String plant = reader.nextLine(); stores one typed line in plant. It writes nothing by itself. System.out.println(plant); is the only writing line. It writes the stored letters and ends the line. The name plant is not written. The name reader is not written. System is not written. The semicolon is not written.

What you type and what the program writes

In the folder that contains this Mint.java, type javac Mint.java. When the file is fine, that command writes no line. An import line and a reader do not make javac write a line, and they do not make javac fail. Then type java Mint. Do not type .java after java. The same two commands are what you type on Windows and on macOS. This page does not quote the shell prompt. The prompts differ, and they are not the program's line.

After java Mint, the program is waiting on reader.nextLine(). It has not finished, and it has not written leaf, because you have not given it a line yet. The cursor sits on a fresh line. Type the letters l, e, a, f. The terminal shows those letters as you press the keys. That showing is the terminal repeating your keys. It is not System.out.println. Then press Enter.

Enter ends the wait. nextLine stores leaf in plant and does not store the Enter. println then writes leaf and ends the line. You can see leaf, and under it leaf again. The first leaf is the keys the terminal showed. The second leaf is the program. The program's line has no quotes. It is four letters, then the line ends. The program finishes after that writing line. If the only leaf you meant to judge is the program's line, judge the line that appears after you press Enter.

If the program's line is plant, the writing line has quotes around plant. The file you keep does not. If the program's line is mint, the writing line is still System.out.println("mint");, or the file still has a quoted word from an earlier page. If java Mint writes leaf at once and never waits, the file still stores a quoted word, such as String plant = "leaf";, and it never calls nextLine. Look for quotes around leaf. The file you keep has none. You do not reinstall Java for a screen that shows the wrong word.

Capitals are copied as you type them. If you type Leaf with a capital L and press Enter, the program's line is Leaf. The file still does not contain those letters. An empty Enter is also a real line. Press Enter without typing letters. nextLine stores no letters in plant. println writes a line with no letters. The program ran. The wait ended because Enter arrived. The file you keep is the one you try with leaf, four small letters.

A space stays in the line

nextLine reads until Enter, not until a space. Type leaf, then a space, then stem, then Enter. The letters stored in plant are leaf, a space, and stem. System.out.println(plant) writes leaf stem. The space is a stored character, the same way a space inside quotes was stored on the last page. This page does not cut the line at the space. The file you keep is still the one-line reader. You only change what you type, not the file, when you want the space on the screen.

Two typed lines, two Enters

One reader can take a second line. Under the first writing line, store a new line in the same name, then write the name again. The new storing line is plant = reader.nextLine();. It does not start with String. plant already exists, so this line only replaces the letters the name holds. The next writing line is again System.out.println(plant);. The program now waits twice. Each nextLine waits for its own Enter.

This is the second file. It is not the file you keep. The file you keep waits once and writes once. Save this second file as Mint.java when you want two typed lines, then put the single nextLine and the single println back.

two typed lines

java
import java.util.Scanner;

public class Mint {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String plant = reader.nextLine();
        System.out.println(plant);
        plant = reader.nextLine();
        System.out.println(plant);
    }
}

Type javac Mint.java. The file is fine, so javac writes no line. The second storing line is allowed to omit String, and that omission does not make javac fail. Then type java Mint. The program waits. Type leaf and press Enter. The terminal shows leaf, and the program writes leaf under it. The program waits again. It has not finished. Type stem and press Enter. The terminal shows stem, and the program writes stem under it. The program's lines are leaf, then stem. The echoed keys sit above each of those lines. If both of the program's lines say leaf, the second nextLine never ran, or you typed leaf both times. If the program writes stem before you have typed a second line, the second println is not sitting under a second nextLine. The screen follows the order of the lines in the file, and each wait follows the nextLine that has not received Enter yet.

Do not write String a second time for the same name. String plant = reader.nextLine();, placed under a line that already created plant, is a different mistake. That line tries to create plant again. javac writes an error and does not stay quiet. You do not need the exact words of that error. The useful fact is that javac wrote a line, so you do not type java Mint yet. Delete the second String and leave plant = reader.nextLine();. The name is created once. Later lines only change the letters it holds. On the file you keep there is no second nextLine at all. After you have seen leaf and then stem, put the one-line file back.

The import line sits above the class

Scanner is not visible to the file until the import line is there. If you delete import java.util.Scanner; and leave the rest of the file you keep, javac writes an error. It does not stay quiet. There is no screen to read, because you do not type java Mint while javac is writing an error. You do not need the exact words of that error. The repair is to put import java.util.Scanner; back as the first line, above public class Mint, with its semicolon. Save, type javac Mint.java again, and wait until javac writes no line.

This third file is the missing import. It is not the file you keep. Save it only until javac writes the error, then put the import line back.

the import line is missing

java
public class Mint {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String plant = reader.nextLine();
        System.out.println(plant);
    }
}

The import line also fails if you tuck it inside main, under the opening brace, or if you place it below the class. Java reads an import above the class, not as a step inside main. Move it back to the first line. A missing semicolon on that line is the same kind of failure: javac writes an error, and you do not run java Mint. The file you keep has the semicolon.

System.in is the only place this page points the reader. Scanner reader = new Scanner(System.out); asks for a reader on the screen. System.out is what println writes to. It is not where typed letters come from. javac writes an error. It does not stay quiet. Do not type java Mint. Change System.out to System.in on that one line. The writing line keeps System.out, because the writing line is the screen. The reader line keeps System.in, because the reader line is the keyboard. Mixing them is the mistake.

This fourth file points the reader at the screen. It is not the file you keep. Save it only until javac writes the error, then change out to in.

the reader pointed at the screen

java
public class Mint {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.out);
        String plant = reader.nextLine();
        System.out.println(plant);
    }
}

The reader has to exist first

Java reads the file from the top. The line that makes the reader has to be above the line that uses reader. If String plant = reader.nextLine(); comes first, and only then Scanner reader = new Scanner(System.in);, the storing line uses the name reader before reader exists. javac writes an error. It does not stay quiet. This is the same order rule as the last page, when plant was used before String plant. The repair is to move the Scanner line above the nextLine line. A name cannot be used on an earlier line than the line that creates it.

This fifth file is the broken order. It is not the file you keep. Save it only until javac writes the error, then put the Scanner line back above nextLine. The import line is still required in a file you actually run. This broken file keeps the import so the only new mistake is the order.

the reader used before it exists

java
import java.util.Scanner;

public class Mint {
    public static void main(String[] args) {
        String plant = reader.nextLine();
        Scanner reader = new Scanner(System.in);
        System.out.println(plant);
    }
}

Type javac Mint.java. javac writes an error. Do not type java Mint. Move Scanner reader = new Scanner(System.in); above String plant = reader.nextLine();. Save. Type javac Mint.java again. Now javac writes no line. Then java Mint waits. Type leaf, press Enter, and the program writes leaf.

Quotes on the writing line still mean what they meant on the last page. After a real nextLine, if the writing line is System.out.println("plant");, you can type leaf and press Enter, and the program's line is still plant. The quotes mark the letters p, l, a, n, t. Java does not look up the name. The line you typed was stored in plant and never written. The repair is the bare name, System.out.println(plant);. The file you keep already has that bare name. Do not put quotes around leaf on the storing line either. String plant = "leaf"; would store leaf from the file and would never wait. This page keeps nextLine on the storing line and keeps the writing line bare.

You can now run Mint.java, wait, type a line, press Enter, and see the program write that line. import java.util.Scanner; sits above the class. Scanner reader = new Scanner(System.in); makes the reader on the keyboard. String plant = reader.nextLine(); stores the line, without the Enter. System.out.println(plant); writes it and ends the line. A later plant = reader.nextLine(); waits for a second Enter. The import, System.in, and the reader-before-use order are what keep javac quiet. The next page leaves the keyboard. It looks at kinds of values a name can hold when you write those values in the file, starting from numbers, not from a line you type.

The screen is the line you typed

The file you keep does not contain leaf. You type leaf after java Mint and press Enter. The terminal shows that typing. The program then writes leaf on the next line. A space you type is part of that line. A second nextLine is a second Enter, and it is not in the file you keep.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

❓
6
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