Scanner class from java.util to read user input from the keyboardnextInt(), nextDouble() and nextBoolean()next().charAt(0), since Scanner has no nextChar() methodnext()next(), which reads one word, and nextLine(), which reads a full linemain()main() methodint, double, char and booleanString class and the charAt() methodimport statementnew keywordmain()System.out.print() and System.out.println()Scanner class from the java.util package.inputAllBaseTypes().Scanner object named scanner to read input from System.in.nextInt() and store it in intValue.intValue.nextDouble() and store it in doubleValue.doubleValue.next(), take its first character using charAt(0), and store it in charValue.charValue.nextBoolean() and store it in booleanValue.booleanValue.next() and store it in stringValue.stringValue.main() method, call inputAllBaseTypes().import java.util.Scanner;
public class InputBaseTypesExample {
public static void inputAllBaseTypes() {
Scanner scanner = new Scanner(System.in);
// Input integer
System.out.print("Enter an integer: ");
int intValue = scanner.nextInt();
System.out.println("Integer Value: " + intValue);
// Input double
System.out.print("Enter a double: ");
double doubleValue = scanner.nextDouble();
System.out.println("Double Value: " + doubleValue);
// Input char
System.out.print("Enter a character: ");
char charValue = scanner.next().charAt(0);
System.out.println("Character Value: " + charValue);
// Input boolean
System.out.print("Enter a boolean (true/false): ");
boolean booleanValue = scanner.nextBoolean();
System.out.println("Boolean Value: " + booleanValue);
// Input string
System.out.print("Enter a string: ");
String stringValue = scanner.next();
System.out.println("String Value: " + stringValue);
}
public static void main(String[] args) {
inputAllBaseTypes();
}
}
25
45.75
A
true
Rumman
Enter an integer: 12
Integer Value: 12
Enter a double: 44.5678
Double Value: 44.5678
Enter a character: r
Character Value: r
Enter a boolean (true/false): true
Boolean Value: true
Enter a string: Ansari
String Value: Ansari
Press any key to continue . . .
This program shows how to read different types of values from the keyboard using Java's
Scanner class. It asks the user for an integer, a double,
a character, a boolean and a string, then prints each value back.
Scanner has a separate method for each data type, except char. To read a character, you read a word and take its first letter.
Makes keyboard input available.
import java.util.Scanner;
Scanner is in the java.util package, so it must be imported before you can use it.
Connects the program to the keyboard.
Scanner scanner = new Scanner(System.in);
System.in is the standard input stream (the keyboard). The scanner object reads from it.
Whole numbers such as 25 or -7.
int intValue = scanner.nextInt();
nextInt() reads the next token and converts it to an int.
Decimal numbers such as 45.75.
double doubleValue = scanner.nextDouble();
nextDouble() reads the next token as a decimal number. If you enter 10, it's stored as 10.0.
There is no nextChar() method.
char charValue = scanner.next().charAt(0);
next() reads a word as a String, and charAt(0) takes its first character.
If the user types Hello, only H is stored.
Only true or false.
boolean booleanValue = scanner.nextBoolean();
nextBoolean() accepts true or false and ignores case, so TRUE also works.
Any other input causes an error.
A single word.
String stringValue = scanner.next();
next() reads characters until it reaches a space. If you enter Rumman Ansari, only Rumman is stored.
The program's starting point.
public static void main(String[] args) {
inputAllBaseTypes();
}
inputAllBaseTypes() is static, so main() can call it directly without creating an object.
| Data Type | Method | Example Input | Stored Value |
|---|---|---|---|
int |
nextInt() |
25 | 25 |
double |
nextDouble() |
45.75 | 45.75 |
char |
next().charAt(0) |
A | 'A' |
boolean |
nextBoolean() |
true | true |
String |
next() |
Rumman | "Rumman" |
Enter an integer: 25
Integer Value: 25
Enter a double: 45.75
Double Value: 45.75
Enter a character: A
Character Value: A
Enter a boolean (true/false): true
Boolean Value: true
Enter a string: Rumman
String Value: Rumman
abc when the program expects an integer throws an InputMismatchException and stops the program.
next() stops at the first space. Use nextLine() to read a full line such as a full name.
nextInt(), the Enter key is left in the input. A following nextLine() reads that empty line instead of waiting for input.
scanner.nextLine(); after nextInt() or nextDouble() before calling nextLine().
scanner.close(); when you've finished reading input, to release the resource.
| Metric | Value | Reason |
|---|---|---|
| Time Complexity | O(1) | The program always reads exactly five values. |
| Space Complexity | O(1) | It uses a fixed number of variables. |
Use nextInt(), nextDouble() and nextBoolean() for numbers and booleans, next().charAt(0) for a character, and next() or nextLine() for text, depending on whether you need one word or a full line.
Time: O(1)
Space: O(1)
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.
Sign in to save your progress and continue exactly where you left off.