简体   繁体   中英

Scanner variable cannot be resolved

In my program, the user will be asked to input 3 integers. The integers will then be read using the Scanner class and listed back to the user.

This is my code:

import java.util.Scanner;

public class Echoer 
{
 public static void main(String[] args) 
 {

  /* The Data Below Will Read The Numbers Input Into The Prompt*/

  Scanner input = new Scanner(System.in);
  System.out.println("Please Enter Three Integers: ");

  int number;
  number = input.nextInt();

  Scan.close();

  System.out.println("Thanks. The Numbers You Entered Are: " + number);

  } 
}

This is the error it returns:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Scan cannot be resolved

Why does it return this error? How can I fix this issue?

In your code, you never defined what Scan was. Use input.close() rather than Scan.close() .

Scan cannot be resolved

means that you never defined Scan . This is because you said Scan.close() . You need to change it to input.close() because input is the name of the instance of the Scanner class.

As others pointed out, you have to close input instead of Scan as shown below.

import java.util.Scanner;

public class Echoer 
{
 public static void main(String[] args) 
 {

/* The Data Below Will Read The Numbers Input Into The Prompt*/

  Scanner input = new Scanner(System.in);
  System.out.println("Please Enter Three Integers: ");

   int number;
   number = input.nextInt();

    input.close();

     System.out.println("Thanks. The Numbers You Entered Are: "+number);

}   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM