[Java] error:can not find symbol. I don't know what the hell is happening

  • Java
  • Thread starter stonecoldgen
  • Start date
  • Tags
    Java Symbol
In Java, variable names are case-sensitive, so hitpoints and hitPoints are two different variables. In summary, the code is trying to initialize and manipulate a variable that hasn't been properly defined, and the variable names are not matching. This will result in an error.
  • #1
stonecoldgen
109
0
Code:
import java.util.Scanner;
import static java.lang.System.out;
import java.util.Random;

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

int hitpoints = 100;
int damage = 40; //un numero cualquiera, solo para hacer una prueba
Scanner keyboard = new Scanner(System.in);

out.println("You have encountered a wild wolf!");
out.println("Would you like to ATTACK or RUN?");

String wolfSituation = keyboard.next();
int wolfHP = 60;
int wolfDMG = 20;  //falta añadir la probabilidad de hit del wolf	


	if (wolfSituation.equals("RUN")){  //al correr, hay que hacer que el jugador queda en la misma casilla pero sin el evento especial
		int randomnessWolfRun = new Random().nextInt(10) + 1;
			if  (randomnessWolfRun <= 5){
				out.println("You have succesfully escaped from the wild wolf.");
			} else{
				int remainingHitPoints = hitPoints - wolfDMG;
				out.println("You have roughly managed to escape, but you have been hurt by the wild wolf.");
			}
	
	}

}
}

on the line
Code:
int remainingHitPoints = hitPoints - wolfDMG;
, this error appears. Actually what I wanted to do was
Code:
int hitPoints = hitPoints - wolfDMG;
because this is a game (just to make the longstory short).


Any help?
 
Technology news on Phys.org
  • #2


I don't know Java, but the obvious guess is that hitpoints and hitPoints are two different variables.
 
  • #3


Looks like a good guess. I don't know Java either, but a Google search for "java case sensitive" returns in the description for the very first hit, "When coding in Java it's important to remember that Java is case sensitive."
 
  • #4


You have not defined the variable remainingHitPoints. Why would you expect the compiler to be able to find it, since it doesn't exist?

Wait ... I see that I'm probably wrong about that. Stating it as int on the same line where you manipulate it must define it in JAVA. If that's the case, then you would have a problem saying

int hitPoints = hitPoints - wolfDMG;

you would need instead to say

hitPoints = hitPoints - wolfDMG;

otherwise you would be double-defining hitPoints (since you defined it at the top of your code)
 
  • #5


As phinds suggests, you can't define and initialize a variable to its own value. You need to do something like this:

int hitPoints = 0;
.
.
.
hitPoints = hitPoints - wolfDMG;

or

hitPoints -= wolfDMG;

Also, as Borek and jtbell point out, hitPoints and hitpoints are different variables.
 

1. What does the "can not find symbol" error mean in Java?

The "can not find symbol" error in Java means that the compiler cannot find a variable, class, or method that has been referenced in the code. This can happen when the name of the symbol is misspelled or when the symbol has not been declared or imported properly.

2. How do I fix the "can not find symbol" error in Java?

To fix the "can not find symbol" error in Java, you should check for any spelling errors in the code and make sure that the symbol has been declared or imported correctly. If the symbol is from an external library, you may need to add the library to the classpath. You may also need to check for any missing semicolons or curly braces in the code.

3. Why am I getting a "can not find symbol" error when the code used to work before?

If the code used to work before but is now giving a "can not find symbol" error, it is likely that the code has been changed or updated and the symbol that is now being referenced is no longer available. Check for any changes in the code and make sure that all necessary symbols are still being imported or declared.

4. Can multiple "can not find symbol" errors occur in the same code?

Yes, it is possible for multiple "can not find symbol" errors to occur in the same code. This can happen if there are multiple misspelled symbols or if there are multiple symbols that have not been properly declared or imported.

5. Is the "can not find symbol" error specific to Java?

The "can not find symbol" error is specific to Java and occurs when the compiler cannot find a symbol that has been referenced in the code. Other programming languages may have similar errors, but they may be named differently.

Back
Top