Reading Input and Strings
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
April 14, 2025
Learn to write a program that reads text written by a user.
Know what a “string” refers to in programming.
Know how to join (i.e., “concatenate”) strings together.
Scanner
tool that comes with Java.import java.util.Scanner;
before the beginning of the main program’s frame (public class
…).Scanner scanner = new Scanner(System.in);
.Scanner
toolBelow is an example of a program which asks for user input, reads the string entered by the user, and then prints it. Try to run this program.
// Introduce the scanner tool used for reading user input
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
// Create a tool for reading user input and name it scanner
Scanner scanner = new Scanner(System.in);
// Print "Write a message: "
System.out.println("Write a message: ");
// Read the string written by the user, and assign it
// to program memory "String message = (string that was given as input)"
String message = scanner.nextLine();
// Print the message written by the user
System.out.println(message);
}
}
Scanner
tool (explanation)scanner
tool’s nextLine()
method.scanner.nextLine()
is left waiting for the user to write something.message
).message
later on – in the example above, the variable message
is referenced in the print command.When the program is run, its output can look like the example below. In this example, the user has written the text “Hello world” – user input is annotated on the right (user input).
The exercise template comes with a program template that includes the creation of a Scanner
tool.
Example output for when the user writes “Bye”.
Example output for when the user writes “Once upon a time…”.
Next up, let’s take a step back, and examine what on earth String message =
… even means.
String
) and its name (myString
, for instance).A string variable called message
that is assigned the value “Hello world!” is declared like this:
Output{.smaller-font-80
Common mistake
A common programming mistake is trying to put quotation marks around variable names. If there were quotation marks around the string variable message
, the program would print the text “message” instead of the “Hello world!” text held by the message
variable.
Output{.smaller-font-80
The string to be printed can be formed from multiple strings using the +
operator.
The same method can be used to join a string literal and the value of a string variable.
public class Program {
public static void main(String[] args) {
String message = "Hello world!";
System.out.println(message + " ... and the universe!");
}
}
Output
We can do the same with any number of strings.
public class Program {
public static void main(String[] args) {
String start = "My name is ";
String end = ", James Bond";
System.out.println(start + "Bond" + end);
}
}
Output
The exercise template contains the following program.
public class HiAdaLovelace {
public static void main(String[] args) {
String name = "Ada Lovelace";
}
}
Modify the program so that it prints the contents of the variable name
, and the printed text is the following in its full form:
Note
When using the System.out.println
command, do not pass in the string “Ada Lovelace” as a parameter. Instead, use the existing variable name
: System.out.println("Hi " + ...)
The reader.nextLine();
command reads the user’s input and returns a string.
If we then want to use the string in the program, it must be saved to a string variable
A value saved to a variable can be used repeatedly.
In the example below, the user input is printed twice.
//Introduce the Scanner tool used for reading
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
//Create the tool for reading, assign it to variable caller "scanner
Scanner scanner = new Scanner(System.in);
//Print user a message "Write a message: "
System.out.println("Write a message: ");
//Read the user's string input, save it to program memory
//"String message = (user input)"
String message = scanner.nextLine();
// Print the user input twice
System.out.println(message);
System.out.println(message);
}
}
System.out.println
command multiple times).The exercise template already includes the code that creates the Scanner
tool.
+
operator.Write a program that prompts the user for their name with the message “What’s your name?”. When the user has written their name, the program has to print “Hi” followed by the user’s name.
The exercise template already includes the code that creates the Scanner
tool.
reader.nextLine()
), the execution stops and waits.Write the first string:
, and then waits for user input.Write the second string:
, and then waits for user input again.import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Write the first string:");
String first = scanner.nextLine();
System.out.println("Write the second string:");
String second = scanner.nextLine();
System.out.println("Write the third string:");
String third = scanner.nextLine();
System.out.println("You wrote:");
System.out.println(first);
System.out.println(second);
System.out.println(third);
}
}
first
, second
and third
)import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Write the first string:");
String first = reader.nextLine();
System.out.println("Write the second string:");
String second = reader.nextLine();
System.out.println("Write the third string:");
String third = reader.nextLine();
System.out.println("You wrote:");
System.out.println(first);
System.out.println(second);
System.out.println(third);
}
}
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Write the first string:");
String first = reader.nextLine();
System.out.println("Write the second string:");
String second = reader.nextLine();
System.out.println("Write the third string:");
String third = reader.nextLine();
System.out.println("Last string you wrote was " + third + ", which ");
System.out.println("was preceded by " + second+ ".");
System.out.println("The first string was " + first + ".");
System.out.println("All together: " + first + second + third);
}
}
Note
The example output might align wrong on narrow displays. If you’re using only a limited portion of the browser window, or your display is otherwise very narrow, try to stretch the display horizontally to see if the text aligns differently. The exercise expects the text to align as it does on wider displays.
Write a program that asks the user for a character’s name and their job. The program then prints a short story.
The output must be as shown in the next tab – note, the name and job depend on the user’s input.
I will tell you a story, but I need some information first.
What is the main character called?
(user input) Legolas
What is their job?
(user input) master archer
Here is the story:
Once upon a time there was Legolas, who was a master archer.
On the way to work, Legolas reflected on life.
Perhaps Legolas will not be a master archer forever.
The exercise template already includes the code that creates the Scanner
tool.
I will tell you a story, but I need some information first.
What is the main character called?
(user input) Ada
What is their job?
(user input) a Data scientist
Here is the story:
Once upon a time there was Ada, who was a a Data scientist.
On the way to work, Ada reflected on life.
Perhaps Ada will not be a a Data scientist forever.
Learn to write a program that reads text written by a user.
Know what a “string” refers to in programming.
Know how to join (i.e., “concatenate”) strings together.
Computer Language