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

  • Thread starter Thread starter stonecoldgen
  • Start date Start date
  • Tags Tags
    Java Symbol
AI Thread Summary
The discussion revolves around a Java programming issue related to variable naming and initialization in a game context. The main problem arises from the line "int remainingHitPoints = hitPoints - wolfDMG;", which leads to confusion due to the case sensitivity of Java. Participants clarify that "hitpoints" and "hitPoints" are treated as distinct variables because Java is case sensitive. The correct approach to update the hit points after an attack is to use "hitPoints = hitPoints - wolfDMG;" or "hitPoints -= wolfDMG;", avoiding the declaration of a new variable. The conversation emphasizes the importance of understanding variable scope and initialization in Java programming.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top