Public static void main ( String args ) Advantage of using Scanner class in java In this program, we use the methods available in Scanner class to read data typed at the keyboard and place it into variables that we specify. We now make a small program that inputs two integers in Java and display result. The variables number1 and string1 can be used as needed in the rest of the program. The following statement reads a string from the console by using the next method and stores it in the variable named string1: If the user enters a value that is different from the type expected (such as a floating-point number), a run-time error result. For instance, the integer value that is returned by the nextInt method copy into another variable called number1 using an assignment statement: The value returned by a method can copy into a variable whose data type matches that of the returned value. Scanner scanner = new Scanner(System.in) We can create a scanner that reads data from the console by using a System.in argument to the Scanner constructor: The nextLine method reads all of the data that a user typed on a line before pressing the Enter key on a keyboard. Each of these methods (except nextLine) reads a set of consecutive characters until whitespace (such as a blank or a tab) encountered. The nextShort, nextFloat, and nextLong methods in Scanner work similarly. The various methods of the scanner class are as follows:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |