Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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); 

    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading