By Dummy for Dummies
What happens when Monkey collects food for the whole jungle? Monkey grabs bananas. Just bananas. Don't be like Monkey.
A good program works for everyone, not just the one case you had in mind. But you don't have everyone's data ahead of time, their name, age, whatever it is. So instead, you ask for it while the program is running. That's input.
There are several ways to take input from the user. The easiest and most common is the Scanner class.
Java doesn't load every feature by default, you pull in only what you need, when you need it. That keeps things light. Scanner is one of those features, and you bring it in with one line:
import java.util.Scanner;
This tells Java to pull Scanner in from its utilities so you can actually use it. But wait, what is a class? How do you use one? Let's talk about that.
Monkey sees a photo of a banana. Monkey cannot eat the photo. But the photo shows the shape and color, everything a real banana should have.
A class works the same way, it's a blueprint. On its own you can't do anything with it. But unlike the banana photo, a class lets you build a real, usable thing from it, called an object. We already imported the class. Now let's create an object from it:
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."
Scannermain keywordscinput or scanner or scnnrrrAs 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:
| Method | Description | Example |
|---|---|---|
nextInt() | Integer (whole number) | 25 |
nextDouble() | Decimal number | 3.14 |
next() | Single word | Coding |
nextLine() | Full line (with spaces) | Coding is fun! |
nextBoolean() | Boolean value | true / false |
next().charAt(0) | Single character | A / % |
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.
// 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); } }
Important Note about nextLine()
Sometimes, right after a number input, Java seems to skip the String input that follows. Here's why: when you type a number and hit enter, Java reads the number, but the enter key itself is still sitting there waiting to be read next.
That's just how Scanner works, not a bug. Fix it by adding sc.nextLine(); right after the number input and before the String input. That extra call eats the leftover enter so it doesn't mess with what comes next. (There's a bit more to it, but this is enough for now.)
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.