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

  • Context: Java 
  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Java Symbol
Click For Summary

Discussion Overview

The discussion revolves around a Java programming error related to variable naming and scope within a simple game code. Participants explore the implications of case sensitivity in variable names and the correct way to update variable values in Java.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the error may stem from the difference between "hitpoints" and "hitPoints," suggesting they are treated as separate variables due to Java's case sensitivity.
  • Another participant agrees with the case sensitivity point and cites a Google search as evidence, emphasizing the importance of this feature in Java.
  • A different participant questions the definition of "remainingHitPoints," initially suggesting it was undefined, but later acknowledges that defining it on the same line should work in Java.
  • One participant clarifies that to update the value of "hitPoints," the correct syntax should be "hitPoints = hitPoints - wolfDMG;" rather than attempting to redefine it with "int hitPoints = hitPoints - wolfDMG;".
  • Another participant reiterates that "hitPoints" and "hitpoints" are indeed different variables, reinforcing the case sensitivity issue.

Areas of Agreement / Disagreement

Participants generally agree on the case sensitivity of variable names in Java and the correct approach to updating variable values. However, there is some uncertainty regarding the definition and initialization of variables in the context of the provided code.

Contextual Notes

There are unresolved issues regarding the variable naming conventions and the implications of defining variables in Java, particularly in relation to scope and initialization.

stonecoldgen
Messages
108
Reaction score
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


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


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


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)
 


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.