Home
/
Programs
/
Write a short Java method, inputAllBaseTypes, that inputs a different value of each base type from the standard input device and prints it back to the standard output device.
Programming Example
Write a short Java method, inputAllBaseTypes, that inputs a different value of each base type from the standard input device and prints it back to the standard output device.
Write a short Java method, inputAllBaseTypes, that inputs a different value of each base type from the standard input device and prints it back to the standard output device.
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 . . .