Input in java step by step
Step 1 : Import utility package in java (package name java.util.Scanner;)
import java.util.Scanner;
Step 2 : Use Scanner Class and Create Object you can give it any name
for creating object of class you have to call the class first and we will call Scanner class
import java.util.Scanner;
public class Input{
public static void main(String args[]){
Scanner // we simply called class
}
}
Step 3 : Create the object of class
for creating object first we will write class name that is Scanner class then we will right object name you can write any object name but the recommened is use "input" as object name
import java.util.Scanner;
public class Input{
public static void main(String args[]){
Scanner input// we simply created object of scanner class
}
}
Step 4 : now use asignment operator and ass new Scanner(system.in)
we will use new keyword because we want to tell system that our this object will take memory or for allocting memory in system and then we will call the constructuer of class that is Scanner() and this constructer takes system.in as argument in input
import java.util.Scanner;
public class Input{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
}
}
Step 5 : Now for geting input we will create a varible with data type in which we will store use given input
import java.util.Scanner;
public class Input{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int number
}
}
Step 6 : after creating variable we will use assignment operator and then write object name that is input and then we will use . operator then well write input type according to datatype
- If you want to get input of single word you will use .next()
- If you want to get input of string can be either a single word or a full paragraph you will use .nextline()
- If you want to get input of single character you will use .nextline().charAt(0)
- If you want to get input of interger number you will use .nextInt()
- If you want to get input of float value you will use .nextFloat()
- If you want to get input of Long value you will use .nextLong()
- If you want to get input of byte value you will use .nextByte()
- If you want to get input of short value you will use .nextShort()
- If you want to get input of double value you will use .nextDouble()
Table
I Hope You All Understant
Download In Pdf here
Download Table here
Final Sample Code Where We will Getting A number In Input
public class Input{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int number= input.nextInt();
}
}
Thankyou

0 Comments