Types

Overview

What is Scanner

How to create Scanner

public class MainApplication {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    }
}

How to read input from scanner

public class MainApplication {
    public static void main(String[] args) {
        OutputStream output = System.out;
        InputStream input = System.in;
        Scanner scanner = new Scanner(input);

        output.println("What is your first name?");
        String firstName = scanner.nextLine();

        output.println("What is your last name?");
        String lastName = scanner.nextLine();

        output.println("What is your email?");
        String name = scanner.nextLine();

        output.println("What is your age?");
        String age = scanner.nextLine();
    }
}

How to not read input from scanner

scanner.next()

scanner.nextInt()

scanner.nextDouble()

Refresher: How to read input from scanner

public class MainApplication {
    public static void main(String[] args) {
        OutputStream output = System.out;
        InputStream input = System.in;
        Scanner scanner = new Scanner(input);

        output.println("What is your first name?");
        String firstName = scanner.nextLine();

        output.println("What is your last name?");
        String lastName = scanner.nextLine();

        output.println("What is your email?");
        String name = scanner.nextLine();

        output.println("What is your age?");
        String age = scanner.nextLine();
    }
}

How to read int from scanner

public class MainApplication {
    public static void main(String[] args) {
        OutputStream output = System.out;
        InputStream input = System.in;
        Scanner scanner = new Scanner(input);

        output.println("What is your age?");
        String ageAsString = scanner.nextLine();
        int age = Integer.parseInt(ageAsString);

        output.println("How many siblings do you have?");
        String siblingsAsString = scanner.nextLine();
        int siblings = Integer.parseInt(siblingsAsString);

        output.println("How many cousins do you have?")
        String cousinsAsString = scanner.nextLine();
        int cousins = Integer.parseInt(cousinsAsString);

        output.println("How many uncles do you have?")
        String unclesAsString = scanner.nextLine();
        int uncles = Integer.parseInt(unclesAsString);

        output.println("How many aunts do you have?")
        String auntsAsString = scanner.nextLine();
        int aunts = Integer.parseInt(auntsAsString);
    }
}

How to read double from scanner

public class MainApplication {
    public static void main(String[] args) {
        OutputStream output = System.out;
        InputStream input = System.in;
        Scanner scanner = new Scanner(input);

        output.println("What is your age?");
        String ageAsString = scanner.nextLine();
        double age = Double.parseDouble(ageAsString);

        output.println("What is 1/2 as a decimal?");
        String halfAsString = scanner.nextLine();
        double half = Double.parseDouble(halfAsString);

        output.println("What is 1/3 as a decimal?");
        String thirdAsString = scanner.nextLine();
        double third = Double.parseDouble(thirdAsString);

        output.println("What is 1/4 as a decimal?");
        String fourthAsString = scanner.nextLine();
        double fourth = Double.parseDouble(fourthAsString);
    }
}

How to simplify Scanner use

public class IOConsole {
    private Scanner scanner;

    public IOConsole() {
        this.scanner = new Scanner(System.in);
    }

    public String getStringInput(String prompt) {
        System.out.println(prompt);
        String userInput = scanner.nextLine();
        return userInput;
    }

    public Double getDoubleInput(String prompt) {
        String userInputAsString = getStringInput(prompt);
        Double userInput = Double.parseDouble(userInputAsString);
        return userInput;
    }

    public Integer getIntegerInput(String prompt) {
        return getDoubleInput(prompt).intValue();
    }
    
}

Refresher: How to read String from IOConsole

public class MainApplication {
    public static void main(String[] args) {
        IOConsole console = new IOConsole();
        String firstName = console.getStringInput("What is your first name?");
        String lastName = console.getStringInput("What is your last name?");
        String email = console.getStringInput("What is your email?");
    }
}

How to read int from IOConsole

public class MainApplication {
    public static void main(String[] args) {
        IOConsole console = new IOConsole();
        int age = console.getIntegerInput("What is your age?");
        int siblings = console.getIntegerInput("How many siblings do you have?");
        int cousins = console.getIntegerInput("How many cousins do you have?")
        int uncles = console.getIntegerInput("How many uncles do you have?")
        int aunts = console.getIntegerInput("How many aunts do you have?")    
    }
}

How to read double from IOConsole

public class MainApplication {
    public static void main(String[] args) {
        IOConsole console = new IOConsole();
        double age = console.getDoubleInput("What is your age?");
        double half = console.getDoubleInput("What is 1/2 as a decimal?");    
        double third = console.getDoubleInput("What is 1/3 as a decimal?");
        double fourth = console.getDoubleInput("What is 1/4 as a decimal?");
    }
}