Java error cannot find symbol scanner

Java error cannot find symbol scanner

I’ve posted about this code earlier today but for a different reason. I honestly have zero clue what to do. I feel like i’ve tried everything. Pls help.

import static java.lang.System.*;

public class TheKingsGuard

public static void main(String[] args)

Scanner sc = new Scanner([System.in](https://System.in));

String Crossbow = «Crossbow»;

System.out.println(«Hello and welcome to The King’s Guard!»);

System.out.println(«To start, please enter your characters details bellow:»);

System.out.println(«Choose a class (Knight, Archer or Mage):»);

while (!Class.equals(«Knight») && !Class.equals(«Archer») && !Class.equals(«Mage»))

System.out.println(«Choose a weapon (Sword, Axe or Mace):»);

String Weapon = sc.nextLine () ;

if (!Weapon.equals(«Sword») && !Weapon.equals(«Axe») && !Weapon.equals(«Mace»))

System.out.println(«That is not a valid weapon.»); >

System.out.println(«Choose a weapon (Bow or Crossbow):»);

String Weapon = sc.nextLine () ;

if (!Weapon.equals(«Bow») && !Weapon.equals(«Crossbow»))

System.out.println(«That is not a valid weapon.»);

String Weapon = sc.nextLine () ;>

String Weapon = sc.nextLine () ;> while (!Weapon.equals(«Wand») && !Weapon.equals(«Staff»)) ;

TheKingsGuard.java:47 : error: cannot find symbol

string Weapon = sc.nextLine () ;

location: class TheKingsGuard

TheKingsGuard.java:49 : error: cannot find symbol

if (!Weapon.equals(«Sword») && !Weapon.equals(«Axe») && !Weapon.equals(«Mace»))

location: class TheKingsGuard

TheKingsGuard.java:49 : error: cannot find symbol

if (!Weapon.equals(«Sword») && !Weapon.equals(«Axe») && !Weapon.equals(«Mace»))

location: class TheKingsGuard

TheKingsGuard.java:49 : error: cannot find symbol

if (!Weapon.equals(«Sword») && !Weapon.equals(«Axe») && !Weapon.equals(«Mace»))

location: class TheKingsGuard

TheKingsGuard.java:55 : error: cannot find symbol

location: class TheKingsGuard

TheKingsGuard.java:55 : error: cannot find symbol

location: class TheKingsGuard

TheKingsGuard.java:55 : error: cannot find symbol

location: class TheKingsGuard

TheKingsGuard.java:63 : error: cannot find symbol

if (!Weapon.equals(«Bow») && !Weapon.equals(«Crossbow»))

location: class TheKingsGuard

TheKingsGuard.java:63 : error: cannot find symbol

if (!Weapon.equals(«Bow») && !Weapon.equals(«Crossbow»))

location: class TheKingsGuard

TheKingsGuard.java:69 : error: cannot find symbol

location: class TheKingsGuard

TheKingsGuard.java:69 : error: cannot find symbol

location: class TheKingsGuard

TheKingsGuard.java:76 : error: cannot find symbol

if (!Weapon.equals(«Staff») && !Weapon.equals(«Wand»))

location: class TheKingsGuard

TheKingsGuard.java:76 : error: cannot find symbol

if (!Weapon.equals(«Staff») && !Weapon.equals(«Wand»))

location: class TheKingsGuard

TheKingsGuard.java:82 : error: cannot find symbol

Читайте также:  Php получить массив свойств объекта

location: class TheKingsGuard

TheKingsGuard.java:82 : error: cannot find symbol

location: class TheKingsGuard

Источник

What is the Cannot Find Symbol Java Error?

Java Course - Mastering the Fundamentals

The Cannot Find Symbol in Java is a compilation error that occurs when we try to refer to something that is not present in the Java Symbol Table. A very common example of this error is using a variable that is not declared in the program.

Java compilers create and maintain Symbol tables. The symbol table stores the information of the identifiers i.e. classes, interfaces, variables, and methods in the given source of code. When we use these identifiers in our code, the compiler looks up to the symbol table for information. If the identifier is not declared or is not present in the given scope the compiler throws ‘Cannot Find Symbol’.

What Causes the Java Cannot Find Symbol Error?

There are multiple ways and reasons this error can occur, they all boil down to the fact that the online Java compiler couldn’t find the identifier in the Symbol table. Some of the very commonly made mistakes are listed below:

  • Java is case sensitive, thus hello and Hello are two different variables.
  • If identifiers are misspelled say the variable’s name is name and we are trying to access nmae can cause an error.
  • Sometimes variables are declared in a particular iterating loop or method and if we try to call them from outside causes an error as it is out of the scope of the variable.
  • Using variables without their declaration is also the common cause of the Java ‘Cannot find Symbol’ error.
  • Sometimes we use methods from classes of a different package. To do so we are required to import the package of the class. Missing import statements can cause the error too.
  • Use of underscore, dollar sign, numbers, letter even a character before or after different from the initial declaration can cause ‘Cannot Find Symbol’.

Examples of Java Cannot Find Symbol error

Let us now see these common errors in the form of code:

Example 1

Let us take a very simple example of a function that takes user input of a number and returns if the number is even or odd.

Читайте также:  Java string array to csv string

In the example above, the Scanner class is not imported and thus it is throwing a Cannot Find Symbol error. The compiler is not able to find Scanner in the program. Once we import the package, it is able to identify the Scanner class.

Example 2

Another example is wrong spelling and undeclared variables. Let us take a simple example where we multiply a variable a few times in a loop.

In the example above we can see 3 errors, all of them being ‘cannot find symbol’. The first one is because N is not declared in the program, and the two are because we declared finalResult but we are calling final_Result . Thus, a single underscore can also cause an error.

Example 3

Lastly, out-of-scope variables also cause a ‘Cannot find Symbol’ error, below example demonstrates it.

In the example above, the sum variable is declared inside the for loop and its scope is only inside for loop. As we are trying to access it outside the for loop, it is causing the error.

Structure of Java Cannot Find Symbol

Look at the output generated by the compiler for the 3rd example:

output-generated-bycompiler

As we can see in the output, the compiler tells the line number of the error as well as points to the variable where the error occurred and the type of the error which is cannot find symbol in this case. The compiler also produces the cannot find symbol including these two additional fields:

  • The very first one is the symbol it is unable to find, i.e. the name and type of the referenced identifier.
  • The other field is the location of the place where the symbol is used. The class and line number are returned where the identifier has been referenced.

Cannot Find Symbol vs Symbol Not Found vs Cannot Resolve Symbol

Different compilers may use slightly different terminologies. The ‘Cannot Find Symbol’, ‘Symbol Not Found’, and ‘Cannot Resolve Symbol’ are all same errors only. Besides the naming, these convey the same meaning to the user and the compiler.

Conclusion

  • The ‘Cannot Find Symbol’ error in Java is a compilation error caused when the compiler cannot find a reference to an identifier.
  • Silly mistakes like case sensitivity, use of underscore, use of undeclared variables, use of out-of-scope variables, etc. can cause this error.
  • It is also identified by ‘Symbol Not Found’ and ‘Cannot Resolve Symbol’.
Читайте также:  Php cmyk to rgb

Источник

What can cause a Cannot find symbol error in java?

Whenever you need to use external classes/interfaces (either user defined or, built-in) in the current program you need to import those classes in your current program using the import keyword.

But, while importing any class −

  • If the path of the class/interface you are importing is not available to JVM.
  • If the absolute class name you have mentioned at the import statement is not accurate (including packages and class name).
  • If you have imported the class/interface used.

You will get an exception saying “Cannot find symbol ……”

Example

In the following example we are trying to read a string value representing the name of the user from key-board (System.in). For this we are using the scanner class of the Java.Util Package.

public class ReadingdData < public static void main(String args[]) < System.out.println("Enter your name: "); Scanner sc = new Scanner(System.in); String name = sc.next(); System.out.println("Hello "+name); >>

Compile time error

Since we are using a class named Scanner in our program and haven’t imported it in our program. On executing, this program generates the following compile time error −

ReadingdData.java:6: error: cannot find symbol Scanner sc = new Scanner(System.in); ^ symbol: class Scanner location: class ReadingdData ReadingdData.java:6: error: cannot find symbol Scanner sc = new Scanner(System.in); ^ symbol: class Scanner location: class ReadingdData 2 errors

Solution

  • You need to set class path for the JAR file holding the required class interface.
  • Import the required class from the package using the import keyword. While importing you need to specify the absolute name (including the packages and sub packages) of the required class.

Example

import java.util.Scanner; public class ReadingdData < public static void main(String args[]) < System.out.println("Enter your name: "); Scanner sc = new Scanner(System.in); String name = sc.next(); System.out.println("Hello "+name); >>

Output

Enter your name: krishna Hello krishna

Источник

Оцените статью