In Java, Scanner is a class used to read user input from the keyboard.
To use it, you must import java.util.Scanner and create a Scanner object.
Scanner is part of the java.util package, so it must be imported before the class.
Use new Scanner(System.in) to read input typed by the user.
Scanner is the class nameinput is the variable nameSystem.in means keyboard inputUse nextLine() to read a full line of text and next() to read only one word.
Scanner has different methods for different data types.
nextInt() – reads an integernextDouble() – reads a decimal numbernextBoolean() – reads true or falsenextLine() – reads a full line of textAfter using nextInt(), nextDouble(), or next(), the leftover newline can cause nextLine() to be skipped.
Fix it by adding an extra nextLine() before reading the full line.
Input values can be stored in variables and used in calculations.
Use methods like hasNextInt() or hasNextDouble() to check input before reading it.
Create a program that asks the user for their name, age, and favorite programming language. Then print a sentence using all three values.
Example Output: Enter your name: Alice Enter your age: 20 Enter your favorite programming language: Java Alice is 20 years old and likes Java.
Hint: If you use nextInt() before nextLine(), remember to clear the leftover newline.
Ask the AI if you need help understanding or want to dive deeper in any topic