This is an old revision of the document!
Table of Contents
J1-1 Strings
Welcome to the first course in Java!
Requirements:
- IntelliJ IDEA CE
You can download IntelliJ IDEA CE from the JetBrains webpage: https://www.jetbrains.com.cn/en-us/idea/download
Scroll down to the second option, the “Community Edition”, which is free.
The Code
Start a new project called “J1-1 Strings”. Then type the following code into the Main.java file:
Main.java
Main.java
public class Main {
public static void main(String[] args) {
// Code starts here:
// Print a message.
System.out.println("Hello, my name is Appledog!");
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Prompt the user for their name
System.out.print("What is your name? ");
// Read the input from the user
String name = scanner.nextLine();
// Output a response
System.out.println("Hello, " + name + "!");
// Close the scanner
scanner.close();
}
}
Now try to run the program. On Mac it's Control-R. On windows, I think, it's Shift-F10. Get used to using a keyboard; it's faster.
Error!
Oh no! Your first error. Find the line with the red underlined word “Scanner”. This tells you that “Scanner” was not found. Click on this word and the IDE will suggest a fix if possible. Here, it can automatically fix the error by inserting a line of code at the top of the program:
import java.util.Scanner
