HIDE NAV

Scanner for User Input on the Command Line

The below source code is a simple command line interface program that uses a Scanner object named scan to take some user input in both text and double precision floating point format.

Main.java


package ceccs;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String textInput;
        double inputValueOne, inputValueTwo, inputSum;
        System.out.println("type a sentence:");
        Scanner scan; // Declaration of Scanner Ojbect. remember to add the import for java.util.Scanner
        scan = new Scanner(System.in);  // Initialization, calling the constructor for Scanner
        textInput = scan.nextLine();
        System.out.println("You typed this stuff:");
        System.out.println(textInput);
        System.out.println("Type in two numbers to add them.");
        inputValueOne = scan.nextDouble();
        inputValueTwo = scan.nextDouble();
        inputSum = inputValueOne + inputValueTwo;
        System.out.println("Here is the sum of your two values:");
        System.out.println(inputSum);
    }
}