How to take user inputs in java
How to take user inputs in java ?we have to import the Scanner class.Scanner class is already defined in java.With the help of Scanner class,it is possible to take user inputs in java programming.In this program, we are going to demonstrate how to take user inputs in java.
First of all use the import statement to import Scanner class which is done by following way.
import java.util.Scanner.*;
After this statement,public class is defined and inside the main method the Scanner object is created by using ‘new’ Keyword with Scanner class which is shown below
Scanner input=new Scanner(System.in);
How to take user inputs in java-is a fun
In the above statement,System.in is called the Input Stream. By the help of input stream,Scanner class is capable of taking the user inputs.In our program,we have created a class called ‘UserInput’ which is a public class.Inside of public class, main method is defined which is
public static void main(String args[])
Then after,three types of variables are created which are integer,string and double in main mehtod.Users are asked to provide inputs into the respective values by using system.out.println method.
When user is asked to input integer value, then user supply that value which is stored into the integer variable.This can be acheived by using the ‘input’ which is an object of Scanner class.The Scanner object known as input calls the nextInt() method of Scanner class which is responsible for capturing the integer value assigned by user and ultimately stored inside the integer varialbe.The following line of codes illustrates the behaviour of how to take user inputs in java.
System.out.println("Please enter your roll number="); roll=input.nextInt();
Similarly,the double and string values are stored into their respective location by the help of Scanner object called ‘input’ which are shown below,
System.out.println("Please enter your name="); name=input.next(); System.out.println("Please enter your marks obtained="); marks_obtained=input.nextDouble();
How to take user inputs in java-complete code
[java]/*How to take user input in java*/
package userinput;
import java.util.Scanner;
/**
*
* @author anchalshrestha
*/
public class UserInput {
public static void main(String[] args) {
// TODO code application logic here
Scanner input=new Scanner(System.in);
int roll;
String name;
double marks_obtained;
System.out.println(“Please enter your roll number=”);
roll=input.nextInt();
System.out.println(“Please enter your name=”);
name=input.next();
System.out.println(“Please enter your marks obtained=”);
marks_obtained=input.nextDouble();
//Now we are printing the user input data
System.out.println(“Your roll number is:”+roll);
System.out.println(“Your name is:”+name);
System.out.println(“Your marks obtained is:”+marks_obtained);
}
}
//Now we have done it, run the program
[/java]
How to take user inputs in java is now easy for all of you guys.If you have any questions about how to take user inputs in java,then feel free to contact me.
How to take user inputs in java-watch video
How to take user inputs in java-Resources
Click Here For The Resources
Click Here For The Resources