INPUT IN JAVA
What happens when Monkey collect food for whole jungle? It will collect just bananas. Do not be like Monkey. Make 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 user right on spot when user is using the program. For that we will set up automatic call to take input from user.
HOW TO TAKE INPUT?
There are plenty of ways to take input from user, the easiest and most common is through 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. Scanner class can also be imported using a small line of code:
import java.util.Scanner;
This tells java to import Scanner from utilities and you can use it then. But wait a minute what is class? How to use it? Let's talk about that....
Class: Monkey see photo of banana, but monkey cannot eat photo, monkey wants real banana but photo shows what is shape and color of banana, photo has all features a banana should have. But we cannot create 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 banana using that class you can create a useable object from it. We imported class and now we will create object using it. Let's create an object:
Creating Scanner object:
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 object says to computer "Hey Mr. Scanner, create new Scanner object that take input into system and name it sc .
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
nextInt() → To input integer (whole number) e.g. 25
nextDouble() → To input decimal number e.g. 3.14
next() → To input single word e.g. Coding
nextLine() → To input full line (along with spaces) e.g. Coding is fun!
nextBoolean() → To input boolean e.g. true / false
next().charAt(0) → To input a single char e.g. A / %
Let's write a simple code for it... The grey part beginning with // are comments we talked about earlier. We are putting them to work to explain code and computer will ignore them and 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(); // only one word
sc.nextLine(); // Explained at the end // Full line input System.out.print("Enter your full address: "); String address = sc.nextLine(); // can have spaces // Character input System.out.print("Enter your grade (A/B/C...): "); char grade = sc.next().charAt(0); // first character of word // Display results System.out.println(age); System.out.println(gpa); System.out.println(firstName); System.out.println(address); System.out.println(grade); } }
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’ll learn how to give output back to the user.
0 Comments