Chapter I - Intro
Chapter I - Intro
If you do not have Java installed on your computer, you can download it for free at oracle.com.
Note: In this tutorial, we will write Java code in a text editor. However, it is possible to write Java in an
Integrated Development Environment, such as IntelliJ IDEA, Netbeans or Eclipse, which are particularly
useful when managing larger collections of Java files.
Setup for Windows
To install Java on Windows:
1. Go to "System Properties" (Can be found on Control Panel > System and Security > System >
Advanced System Settings)
2. Click on the "Environment variables" button under the "Advanced" tab
3. Then, select the "Path" variable in System variables and click on the "Edit" button
4. Click on the "New" button and add the path where Java is installed, followed by \bin. By default,
Java is installed in C:\Program Files\Java\jdk-11.0.1 (If nothing else was specified when you
installed it). In that case, You will have to add a new path with: C:\Program Files\Java\jdk-
11.0.1\bin
Then, click "OK", and save the settings
5. At last, open Command Prompt (cmd.exe) and type java -version to see if Java is running on
your machine
Java User Input
The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class,
create an object of the class and use any of the available methods found in the Scanner class documentation. In
our example, we will use the nextLine() method, which is used to read Strings:
Example
import java.util.Scanner; // Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}}
Input Types
In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at
the table below:
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
In the example below, we use different methods to read data of various types:
Example
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error message
(like "InputMismatchException").
Java Output / Print
Print Text
You learned from the previous chapter that you can use the println() method to output values or print text
in Java:
Example
System.out.println("Hello World!");
You can add as many println() methods as you want. Note that it will add a new line for each method:
Example
System.out.println("Hello World!");
System.out.println("I am learning Java.");
System.out.println("It is awesome!");
Double Quotes
When you are working with text, it must be wrapped inside double quotations marks "".
If you forget the double quotes, an error occurs:
Example
System.out.println("This sentence will work!");
System.out.println(This sentence will produce an error);
The Print() Method
There is also a print() method, which is similar to println().