05 Java Fundamentals | Input in Java | By Dummy for Dummies

Java Input | Hassan Bukhari
Java Notes

05 Java Fundamentals | Input in Java | By Dummy for Dummies

Java Beginner Guide Scanner User Input

INPUT IN JAVA

What happens when Monkey collects food for the whole jungle? It will collect just bananas. Do not be like Monkey. Make a program that everyone can use. But the problem is that you do not have data about everyone, their names, age etc. Therefore we will take input from the user right on the spot when the user is using the program. For that we will set up an automatic call to take input from the user.


HOW TO TAKE INPUT?

There are plenty of ways to take input from the user; the easiest and most common is through the Scanner class. Java loads only some important features when you are coding in it, and it allows you to load as many as you need during coding. This is to not put unnecessary burden on your system. The Scanner class can also be imported using a small line of code:

Java
import java.util.Scanner;

This tells Java to import Scanner from utilities and you can use it then. But wait a minute, what is a class? How to use it? Let's talk about that...

Class

Monkey sees a photo of a banana, but monkey cannot eat the photo. Monkey wants a real banana, but the photo shows what is the shape and color of the banana. The photo has all the features a banana should have. But we cannot create a real banana from the photo.

Class is like a blueprint/photo/map. When you have a class for something, you cannot do anything with it, but unlike the banana, using that class you can create a usable object from it. We imported the class and now we will create an object using it. Let's create an object:

Creating Scanner object

Java
import java.util.Scanner;

public class InputInt {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  // This line created object
    }
}

The line which created the object says to the computer: "Hey Mr. Scanner, create a new Scanner object that takes input into the system and name it sc."

Points to Remember

  • Class is just a blueprint Scanner
  • Every class name starts with a capital letter; object name must NOT start with a capital letter
  • Class is imported at the top of the code even before the greyed lines
  • Object is created after the line with main keyword
  • We will use the object, e.g., sc
  • You can give any name to the object, e.g., input or scanner or scnnrrr
  • The object name must not be exactly the same as the class name (Scanner and scanner are different)
  • Repeat the same process and exact code every time you want to create a Scanner object

Taking input from user

As we know there are many types of variables and many datatypes. Therefore taking input for each is a bit different. Here is a list for each:

MethodDescriptionExample
nextInt()Integer (whole number)25
nextDouble()Decimal number3.14
next()Single wordCoding
nextLine()Full line (with spaces)Coding is fun!
nextBoolean()Boolean valuetrue / false
next().charAt(0)Single characterA / %

Let's write a simple code for it... The green part beginning with // are comments we talked about earlier. We are putting them to work to explain the code, and the computer will ignore them while the remaining will be read.

Java
// importing Scanner
import java.util.Scanner;

// Start of the program
public class Main {
    public static void main(String[] args) {
        // creating Scanner object
        Scanner sc = new Scanner(System.in);

        // Integer input
        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        // Double input
        System.out.print("Enter your GPA: ");
        double gpa = sc.nextDouble();

        // Word input
        System.out.print("Enter your first name: ");
        String firstName = sc.next();

        sc.nextLine();  // Consumes the leftover newline

        // Full line input
        System.out.print("Enter your full address: ");
        String address = sc.nextLine();

        // Character input
        System.out.print("Enter your grade (A/B/C...): ");
        char grade = sc.next().charAt(0);

        // Display results
        System.out.println(age);
        System.out.println(gpa);
        System.out.println(firstName);
        System.out.println(address);
        System.out.println(grade);
    }
}
Console Output
Enter your age: 20 Enter your GPA: 3.75 Enter your first name: Hassan Enter your full address: Katlang, Mardan Enter your grade (A/B/C...): A 20 3.75 Hassan Katlang, Mardan A
Important Note about nextLine()
Sometimes after taking a number input, Java skips your coming String input. That is because you write a number and then hit the "enter" button. So Java sends the number into the number input and the "enter" into your upcoming String input. This is not your fault; it is an issue with Java. To avoid that, you put sc.nextLine(); after the number input and before the String input. It will eat the next line waiting to disturb your code and clean it for you. (It's a bit more complicated, but this much is quite enough for now).

CLOSING

That's it for taking input in Java! By using the Scanner, we can collect numbers, words, sentences, and even single characters right from the keyboard. In the next blog, we will learn how to give output back to the user.