Importance of User Input
User input is an entry, in any form, made by the user, that will be used in the program. User input is one of the most important aspects of programming. Most programs don’t run and function without the presence of a user’s input. Every program must have a form of user interaction, whether it be getting the name of a player for a game, or asking a member for their login credentials. In this article, we will discuss the basics of user input. The code examples may vary from compiler to compiler.
User Input in Java
In Java programming, the “Scanner” class is used in order to get an input from the user, and it can be found in the java.util package. To implement and use the Scanner class, an object must be created, which belongs to the class, and as a result, it can use any of the available methods found in the Scanner class documentation.
There are multiple input types that can be entered by the user, but in order to restrict the user to a certain type of entry, some methods are used. They are as follows:
nextBoolean() - This requires the user to enter a boolean value (a data type that has one of two possible values (most commonly true and false))
nextByte() - This reads a byte value from the user (byte data type is an 8-bit signed two's complement integer)
nextDouble() - This reads a double value from the user (double data type is a double-precision 64-bit IEEE 754 floating point)
nextFloat() - This reads a float value from the user (floating point numbers represent the numbers which contain a fractional part, with one or more decimals)
nextInt() - This reads an integer value from the user (a data type that represent some range of mathematical integers)
nextLine() - This reads a string value from the user (a string data type is used to represent text)
nextLong() - This reads a long value from the user (long data type is a 64-bit two's complement integer)
nextShort() - This reads a short value from the user (short data type is a 16-bit signed two's complement integer)
Article was written for CyberClubNPSi
Comentarios