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();
}
}
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 . . .
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.