Different types of Input, "scanner cannot be resolved" in Java

I seem to have a problem with the 15th exercise of MOOC course Java Programming I. When I try to run the code, VSC tells me "scanner cannot be resolved" concerning all of the scanners. But previously these same lines worked in separate exercises. So I am not really sure what is wrong here.

If I replace String text = scanner.nextLine(); with String text = String.valueOf(scan.nextLine()); then this scanner starts to work without issues but the rest of them return the same error as before.

import java.net.SocketTimeoutException;
import java.util.Scanner;

public class DifferentTypesOfInput {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Give a string:");
        String text = scanner.nextLine();
        System.out.println("Give an integer:");
        int value = Integer.valueOf(scanner.nextLine());
        System.out.println("Give a number:");
        double decimle = Double.valueOf(scanner.nextLine());
        System.out.println("Write something:");
        boolean tof = Boolean.valueOf(scanner.nextLine());
        System.out.println("You gave the string " + text);
        System.out.println("You gave the integer " + value);
        System.out.println("You gave the double " + decimle);
        System.out.println("You gave the boolean " + tof); 

    }
}

>Solution :

Well, for one, you’ve declared the Scanner object to be identified by the variable name "scan" yet you’re trying to call Scanner methods on the variable name "scanner" which you haven’t declared or defined.

Leave a Reply