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.

Java Programming Language DataTypes in java (Article) DataTypes in java (Program)

114

Program:

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

Output:

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 Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.