Java User Input


Java User Input

In this tutorial we will learn how to take input from the user in Java. We will use the scanner class. It is available in the java.util package. In the following example we will use the nextLine() function-

Program
import java.util.*;
public class Main
{

     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter your first name");
        String fname = sc.nextLine();
        System.out.println("Enter your last name");
        String lname = sc.nextLine();
        System.out.println("FullName = "+fname+" "+lname);
     }
}
Run

nextBoolean()

nextBoolean() This function reads the Boolean value from the user. Boolean value means true, false.

Program
import java.util.*;
public class Main
{

     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter your boolean value");
        boolean inputValue = sc.nextBoolean();
        if(inputValue)
        {
            System.out.println("Your entered value is true");
        }
        else
        {
            System.out.println("Your entered value is false");
        }
     }
}
Run

nextByte()

nextByte() This function reads the byte value from the user.

Program
import java.util.*;

public class Main {
	public static void main(String[] args)
	{

		String s = "hkk 9 + 6 = 19.0";
		Scanner sc = new Scanner(s);
		while (sc.hasNext())
		{
			if (sc.hasNextByte()) 
			{
				System.out.println("Found Byte value :" + sc.nextByte(12));
			}
			else 
			{
				System.out.println("Not found Byte value :" + sc.next());
			}
		}
	}
}
Run

nextDouble()

nextDouble() This function reads the double value from the user.

Program
import java.util.*;

public class Main {
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your first double value");
    double firstInput = sc.nextDouble();
    System.out.println("Enter your second double value");
    double secondInput = sc.nextDouble();
    double Sum = firstInput + secondInput;
    System.out.println("Sum = "+Sum);
}
}
Run

nextFloat()

nextFloat() This function reads the float value from the user.

Program
import java.util.*;

public class Main {
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your first float value");
    float firstInput = sc.nextFloat();
    System.out.println("Enter your second float value");
    float secondInput = sc.nextFloat();
    float Sum = firstInput + secondInput;
    System.out.println("Sum = "+Sum);
}
}
Run

nextInt()

nextInt() This function reads the integer value from the user.

Program
import java.util.*;

public class Main {
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your first Integer value");
    int firstInput = sc.nextInt();
    System.out.println("Enter your second Integer value");
    int secondInput = sc.nextInt();
    int Sum = firstInput + secondInput;
    System.out.println("Sum = "+Sum);
}
}
Run

nextLong()

nextLong() This function reads the long value from the user.

Program
import java.util.*;

public class Main {
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your first long value");
    long firstInput = sc.nextLong();
    System.out.println("Enter your second long value");
    long secondInput = sc.nextLong();
    long Sum = firstInput + secondInput;
    System.out.println("Sum = "+Sum);
}
}
Run

nextShort()

nextShort() This function reads the short value from the user.

Program
import java.util.*;

public class Main {
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your short value");
    short firstInput = sc.nextShort();
    System.out.println("Short value = "+firstInput);
}
}
Run