codescracker


java

Java Program to Get Input from User



« Previous Program Next Program »


Get Input from User

To get input from the user in Java programming, first you have to import the java.util.Scanner package that contains Scanner class which helps you to get the input from the user as shown in the following program.

Java Programming Code to Get Input from User

Following are the one by one Java programs to get the integer, character, float, and string input from the user.

Get Integer Input from User

/* Java Program Example - Get Integer Input from User */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int num;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        System.out.print("You entered " +num);
    }
}

When the above Java Program is compile and executed, it will produce the following output. Above Java Programming Example Output (for integer):

get integer input in java

Get Character Input from User

/* Java Program Example - Get Character Input from User */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        char ch;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Character : ");
        ch = scan.next().charAt(0);
		
        System.out.print("You entered " +ch);
    }
}

Above Java Programming Example Output (for character):

get character input in java

Get Float Input from User

/* Java Program Example - Get Float Input from User */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        float flt;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Floating Point Number : ");
        flt = scan.nextFloat();
		
        System.out.print("You entered " +flt);
    }
}

Above Java Programming Example Output (for float):

get float input in java

Get String Input from User

/* Java Program Example - Get String Input from User */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        String str;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter String : ");
        str = scan.nextLine();
		
        System.out.print("You entered " +str);
    }
}

Above Java Programming Example Output (for string):

get string input in java

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test