Basic I/O Lesson

Communicating with Your Program

Input/Output (I/O) is the bridge between your program and the outside world. It allows your code to display information (output) and receive data from a user (input), making your applications interactive and dynamic.

Standard Output: Displaying Information

The simplest way for your program to communicate is by printing text to the console. In FRC, this output is sent directly to the Driver Station logs, making it an essential tool for debugging. Java provides two primary methods for this.

// 1. System.out.println()
// Prints the given text and then moves the cursor to a new line.
System.out.println("Robot initialization complete.");
System.out.println("Arm Angle: 45.0 degrees");

// 2. System.out.print()
// Prints the given text but keeps the cursor on the same line.
System.out.print("Motor Speed: ");
System.out.print("0.75");
// Output would be: Motor Speed: 0.75

Standard Input: Reading User Data with `Scanner`

To make your programs interactive, you need a way to get input from the user. The `Scanner` class is Java's primary tool for reading input from the console. Using it involves three main steps.

Steps for Using `Scanner`

  1. Import the Class: At the very top of your file, add `import java.util.Scanner;` to tell Java you want to use this tool.
  2. Create a Scanner Object: In your method, create an instance of the scanner linked to the keyboard: `Scanner inputReader = new Scanner(System.in);`
  3. Use Scanner Methods: Call methods like `nextInt()`, `nextDouble()`, or `nextLine()` to read different types of data.
import java.util.Scanner; // Step 1: Import

public class UserInputExample {
    public static void main(String[] args) {
        // Step 2: Create a Scanner object
        Scanner userInput = new Scanner(System.in);

        // --- Reading a String ---
        System.out.print("Enter your team's name: ");
        String teamName = userInput.nextLine(); // Reads the entire line of text
        
        // --- Reading an Integer ---
        System.out.print("Enter your target shooter RPM: ");
        int shooterRpm = userInput.nextInt(); // Reads a whole number

        // --- Reading a Double ---
        System.out.print("Enter desired drive speed (-1.0 to 1.0): ");
        double driveSpeed = userInput.nextDouble(); // Reads a decimal number

        System.out.println("--- Configuration ---");
        System.out.println("Team: " + teamName);
        System.out.println("Shooter Target: " + shooterRpm + " RPM");
        System.out.println("Drive Speed: " + driveSpeed);

        // It's good practice to close the scanner when you're done.
        userInput.close();
    }
}

The `nextInt()` and `nextLine()` Trap

A very common issue for new programmers: after you call `nextInt()` or `nextDouble()`, the "Enter" key press is left behind in the input stream. If you immediately call `nextLine()` after, it will read that leftover "Enter" and give you an empty string.

The Fix: After reading a number, add an extra `userInput.nextLine();` to consume the leftover newline character before you try to read the next line of text.

Test Your Knowledge

Question: You need to write a program that asks a user to enter their robot's weight, which could be a decimal number (e.g., 124.5). Which `Scanner` method is the most appropriate for reading this value?