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

Points to Remember: 
⦿ Class is just a blueprint
⦿ Every class name starts with capital, object must NOT start with capital
⦿ Class is imported in top of code even before the greyed lines. 
⦿ Object is created after the line with  main  keyword
⦿ We will use object e.g.  sc 
⦿ You can give any name to object e.g.  input  or  scanner  or  scnnrr  
⦿ The object name must not be exactly same as class name
     (Scanner and scanner are different)
⦿ Repeat the same process and exact code everytime you want to create 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

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); } }


Sometimes after taking a number input, Java skips your coming String input. That is because you write number and then hit "enter" button. So java sends number into number input and the "enter" into your upcoming String input. This is not your fault it is an issue with java, so to avoid that you put sc.nextLine(); after number input and before String input. It will eat the next line waiting to disturb your code and clean it for you. (It's a bit more complicated though, 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’ll learn how to give output back to the user.