User input in Java: A user input can be used dynamically enter data by user. Java provide Scanner is a predefined class which is used to take user input in java. Scanner class is defined inside java.util package. Scanner class consists of many function to take user input in java.
Types of User Input in Java
- nextInt(): A nextInt function can be used to take integer value from the user.
- nextFloat(): A nextFloat function can be used to take float value from the user.
- nextDouble(): A nextDouble function can be used to take double value from the user.
- next():A next function can be used to take string value without space from the user.
- nextLine():A nextLine Function can be used to take string value from the user.
- nextByte(): A next byte function can be used to take byte value from the user.
- nextShort(): A next short function can be used to take short value from the user.
- nextLong(): A nextLong function can be used to take long value from the user.
How to Take Integer User Input in Java
class Jiocoding{ public static void main(String[] args) int x,y Scanner s = new Scanner(System.in); System.out.print("Enter x Value: "); x = s.nextInt(); System.out.print("Enter y Value: "); y = s.nextInt(); System.out.println("Value of X+Y:" + (x+y)); } } ============OUTPUT============ Enter Value of X: 40 Enter Value of Y:35 Value of X+Y:75 |
Float Input: A float input can be used floating decimal value from the user. Float input can be define Scanner Java class.
How to Take Float User Input in Java
class Jiocoding{ public static void main(String[] args) float x,y Scanner s = new Scanner(System.in); System.out.print("Enter x Value: "); x = s.nextFloat(); System.out.print("Enter y Value: "); y = s.nextFloat(); System.out.println("Value of X+Y:" + (x+y)); } } ============OUTPUT============ Enter Value of X: 5.7 Enter Value of Y:5.9 Value of X+Y:11.6 |
Double Input: A double input can be used double long decimal value from the user. Double input can be define Scanner Java class.
How to Take Double User Input in Java
class Jiocoding{ public static void main(String[] args) double x,y Scanner s = new Scanner(System.in); System.out.print("Enter x Value: "); x = s.nextFloat(); System.out.print("Enter y Value: "); y = s.nextDouble(); System.out.println("Value of X+Y:" + (x+y)); } } ============OUTPUT============ Enter Value of X: 546.54 Enter Value of Y:454.65 Value of X+Y:1001.1899780273437 |
Next Input: A next input can be used string value from the user. String input can be define Scanner Java class.
How to Take Next User Input in Java
class Jiocoding{ public static void main(String[] args) String name; Scanner s = new Scanner(System.in); System.out.print("Enter City Name: "); name = s.next(); System.out.println("City Name:" +name); } } ============OUTPUT============ Enter City Name: Noida City Name: Noida |
Line Input: A Line input can be used taking string value from the user. NextLine input can be define Scanner Java class.
How to Take NextLine User Input in Java
class Jiocoding{ public static void main(String[] args) Scanner s = new Scanner(System.in); System.out.print("Enter Website Name: "); String name = s.nextLine(); System.out.println("You Can Visit Website:" + name); } } ============OUTPUT============ Enter Website Name: jiocoding You Can Visit Website: jiocoding |
Byte Input: A Byte input can be used integer byte value from the user. nextByte input can be define Scanner Java class.
How to Take NextByte User Input in Java
class Jiocoding{ public static void main(String[] args) byte x; Scanner s = new Scanner(System.in); System.out.print("Enter Byte Value: "); x = s.nextByte(); System.out.println("Value of Byte:" + x); } } ============OUTPUT============ Enter Byte Value: 509 Value of Byte: 500 |
Short Input: A short input can be used short integer value short 16 bit signed from the user. Short input can be define Scanner Java class.
How to Take NextShort User Input in Java
class Jiocoding{ public static void main(String[] args) short x; Scanner s = new Scanner(System.in); System.out.print("Enter Short Value: "); x= s.nextShort(); System.out.println("Value of Short:" + x); } } ============OUTPUT============ Enter Short Value: 9432 Value of Short:9432 |
Long Input: A Long input can be used long value from the user.It can be taking 8-byte. NextLong input can be define Scanner Java class.
How to Take NextLong User Input in Java
class Jiocoding{ public static void main(String[] args) long phone; Scanner s = new Scanner(System.in); System.out.print("Enter Phone No: "); phone = s.nextLong(); System.out.println("Phone Number is:" + phone); } } ============OUTPUT============ Enter Phone Number: 9696969696 Phone Number is: 9696969696 |
Second Method to take user input in Java
BufferedReader: BufferedReader is a predefined class which is used to take user input in java. It is defined inside the java.io package.
How to Take BufferedReader User Input in Java
import java.io.InputStreamReader; class Jiocoding{ public static void main(String[] args) BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String name; try{ System.out.println("Enter Website Name"); name=reader.readLine(); System.out.println("Website Name is:"+name); } catch (Exception e) { } } } ============OUTPUT============ Enter Website Name: Jiocoding Website Name is: Jiocoding |

0 Comments