|
| 1 | +# Java Tutorial: Getting User Input in Java |
| 2 | + |
| 3 | +### Reading data from the Keyboard : |
| 4 | +- Scanner class of java.util package is used to take input from the user's keyboard. |
| 5 | +- The Scanner class has many methods for taking input from the user depending upon the type of input. |
| 6 | +- To use any of the methods of the Scanner class, first, we need to create an object of the Scanner class as shown in the below example : |
| 7 | + |
| 8 | +``` |
| 9 | +import java.util.Scanner; // Importing the Scanner class |
| 10 | +Scanner sc = new Scanner(System.in); //Creating an object named "sc" of the Scanner class. |
| 11 | +``` |
| 12 | + |
| 13 | +- Taking an integer input from the keyboard : |
| 14 | + |
| 15 | +``` |
| 16 | +Scanner S = new Scanner(System.in); //(Read from the keyboard) |
| 17 | +int a = S.nextInt(); //(Method to read from the keyboard) |
| 18 | +``` |
| 19 | + |
| 20 | +### Code as Described in the Video: |
| 21 | + |
| 22 | +``` |
| 23 | +package com.company; |
| 24 | +import java.util.Scanner; |
| 25 | +
|
| 26 | +public class CWH_05_TakingInpu { |
| 27 | + public static void main(String[] args) { |
| 28 | + System.out.println("Taking Input From the User"); |
| 29 | + Scanner sc = new Scanner(System.in); |
| 30 | +// System.out.println("Enter number 1"); |
| 31 | +// int a = sc.nextInt(); |
| 32 | +// float a = sc.nextFloat(); |
| 33 | +// System.out.println("Enter number 2"); |
| 34 | +// int b = sc.nextInt(); |
| 35 | +// float b = sc.nextFloat(); |
| 36 | +
|
| 37 | +// int sum = a +b; |
| 38 | +// float sum = a +b; |
| 39 | +// System.out.println("The sum of these numbers is"); |
| 40 | +// System.out.println(sum); |
| 41 | +// boolean b1 = sc.hasNextInt(); |
| 42 | +// System.out.println(b1); |
| 43 | +// String str = sc.next(); |
| 44 | + String str = sc.nextLine(); |
| 45 | + System.out.println(str); |
| 46 | +
|
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Handwritten Notes: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-5/JavaChapter1.pdf) |
| 52 | + |
| 53 | +### Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-5/UltimateJavaCheatSheet.pdf) |
0 commit comments