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.

155 Views
Java
Beginner
Code Snippet
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.

Before You Begin

What you will learn

  • Use the Scanner class from java.util to read user input from the keyboard
  • Read integer, double and boolean values with nextInt(), nextDouble() and nextBoolean()
  • Read a single character with next().charAt(0), since Scanner has no nextChar() method
  • Read a single-word string with next()
  • Understand the difference between next(), which reads one word, and nextLine(), which reads a full line
  • Organise input logic in a separate static method and call it from main()

Prerequisites

  • Basic Java syntax and how to write the main() method
  • Primitive data types: int, double, char and boolean
  • The String class and the charAt() method
  • Importing packages with the import statement
  • Creating objects with the new keyword
  • Static methods and how to call them from main()
  • Printing output with System.out.print() and System.out.println()

📌 Information & Algorithm

Algorithm

  1. Start
  2. Import the Scanner class from the java.util package.
  3. Define a static method inputAllBaseTypes().
  4. Create a Scanner object named scanner to read input from System.in.
  5. Display the message "Enter an integer:".
  6. Read an integer using nextInt() and store it in intValue.
  7. Display intValue.
  8. Display the message "Enter a double:".
  9. Read a decimal number using nextDouble() and store it in doubleValue.
  10. Display doubleValue.
  11. Display the message "Enter a character:".
  12. Read a word using next(), take its first character using charAt(0), and store it in charValue.
  13. Display charValue.
  14. Display the message "Enter a boolean (true/false):".
  15. Read a boolean using nextBoolean() and store it in booleanValue.
  16. Display booleanValue.
  17. Display the message "Enter a string:".
  18. Read a single word using next() and store it in stringValue.
  19. Display stringValue.
  20. In the main() method, call inputAllBaseTypes().
  21. Stop
Steps 5 to 19 follow the same pattern for each data type: prompt → read → store → display.

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

Sample Input

25
45.75
A
true
Rumman

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

📘 Explanation

Overview

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.

Step-by-Step Explanation

1

Import the Scanner Class

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.

2

Create a Scanner Object

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.

3

Read an Integer

Whole numbers such as 25 or -7.

int intValue = scanner.nextInt();

nextInt() reads the next token and converts it to an int.

4

Read a Double

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.

5

Read a Character

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.

6

Read a Boolean

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.

7

Read a String

A single word.

String stringValue = scanner.next();

next() reads characters until it reaches a space. If you enter Rumman Ansari, only Rumman is stored.

8

Call the Method from main()

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.

Scanner Methods Used

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"

Sample Run

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

Common Mistakes

Wrong input type Typing abc when the program expects an integer throws an InputMismatchException and stops the program.
Expecting a full sentence from next() next() stops at the first space. Use nextLine() to read a full line such as a full name.
Mixing nextInt() and nextLine() After nextInt(), the Enter key is left in the input. A following nextLine() reads that empty line instead of waiting for input.
Fix Add an extra scanner.nextLine(); after nextInt() or nextDouble() before calling nextLine().
Good practice Close the scanner with scanner.close(); when you've finished reading input, to release the resource.

Complexity

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.

Key Takeaway

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.

Complexity

Time: O(1)

Space: O(1)

📚 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.