Home Java Programming Language / 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.

👁 124 Views
💻 Practical Program
📘 Step Learning
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.

💻 Program Code

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

                        

🖥 Program 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 . . .

                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.