• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Eclipse Error - i cannot be resolved to a variable

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help!  I am working on my simple hangman code for my basic Java class.  I am using eclipse.  I am getting the following error when I try to replace the underscore with the correct characters.  What have I done wrong?

import java.util.Arrays;
import java.util.Scanner;

public class Hangman {
 

public static void main(String[] args) {

WordInfo wordInfo = new WordInfo("wordlist.xml");
String theWord = wordInfo.getTheWord();
int numLetters = wordInfo.getNumLetters();
char []correctGuess = new char[numLetters];
System.out.println(theWord);
System.out.println(numLetters);

//Splitting the word into characters
String[] letters = theWord.split("");
//System.out.println(letters.length);

//Displaying the underscores
for(int i = 0; i <letters.length; i++) {
System.out.print("_ ");
correctGuess[i] = '_';
}

System.out.print("\n");
Scanner guess = new Scanner(System.in);

boolean lives = true;
boolean win = false;
int wrong = 6;

//Game loop - Checking for available lives and to see if the player won
while(lives && win == false) {


System.out.print("\nGuess a letter: ");
String theGuess = guess.nextLine();

boolean correct = Arrays.asList(letters).contains(theGuess);
char guessChar = theGuess.charAt(0);

if(correct) {
//System.out.println("Letter is in word");
char[] chars = theWord.toCharArray();

//Replacing the underscore with the correct character
if (chars[i]==guessChar) {
correctGuess[i] = guessChar;
}
}

boolean flag = true;

//Checking to see if the player won the game to see if there are any underscores
for (char ch:correctGuess) {
System.out.print(ch+" ");
if (ch == '_') {
flag = false;
}
}
win = flag;

/**for(char ch:chars) {
if(ch == guessChar) {
System.out.print(guessChar);

}
else {
System.out.print("_ ");

}
}*/
}
else {
System.out.println("Letter is not in the word");
wrong --;
System.out.println("Lives left " + wrong);
if(wrong <1) {
System.out.println("You lose");
lives = false;
}
}

if (win == true) {
System.out.println("\nYou win!");
}
}
}
 
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What source line is the error on?  Can you mark the line with an easy to see comment:  <<<<<<<< HERE

Also please wrap the source code in code tags: Select the code and press the Code button.
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or even better, wrap your code in the code tags.  

Welcome to the Ranch, by the way
 
Roxanne Booth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two places were I commented '//ERROR HERE'.  Thank you for the assistance.

 
Norm Radder
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There are two places were I commented '//ERROR HERE'


The comment // ERROR HERE is NOT on any source line.  Could you put the marker on the line with the error?

Also please wrap the source code in code tags.  Select ALL of the code and press the Code button.  Press Preview to see if the tags are properly placed.
 
Roxanne Booth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what code tags are.  I am 3 weeks into my Java course and we haven't covered them.  This is the first error.

//ERROR HERE
//Replacing the underscore with the correct character
if (chars[i]==guessChar) {
correctGuess[i] = guessChar;
}

Eclipse is giving me an error (red underline) on both of the 'i' in the array.  Error says 'i cannot be resolved to a variable' for both values of 'i'
 
Norm Radder
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

'i cannot be resolved to a variable'


The compiler can not find a definition for the variable: i that is in scope where the error message is.
Variables must be defined before they are used.
Variables defined in an for loop header are only in scope inside the loop.

If you want to save the value from the for loop, declare the variable outside of the loop.  It can still be used inside of the loop.


The above is wrapped in code tags.  Select the text and press the Code button.  See the first part of your post on Today 2:31:53 PM.  It looks like it is in code tags.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a code button up on the row where you have all the buttons (to make text bold, italic, underline,...). I have attached a screenshot of how it works.
Screenshot-2019-09-29-at-20.46.20.png
[Thumbnail for Screenshot-2019-09-29-at-20.46.20.png]
 
Roxanne Booth
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My eclipse does not have items like 'bold' in the top nav bar.  When I try to use the buttons in this forum all it puts are things like:



 
Norm Radder
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 When I try to use the buttons in this forum all it puts are things like:


Select ALL of the code BEFORE pressing the Code button.

To Select anything,
move the cursor to the top left of the text to be selected.
Press the mouse's left button
drag the cursor to the bottom right of the text to be selected.
release the left button
The selected text should show in BLUE.

When all is selected, press the Code button.

Use the Preview button below the input box to see what the post will look like.  If it looks OK press the Submit button.
Otherwise fix the contents of the post before pressing Submit.
 
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only place that you have defined an 'i' variable is inside a for() loop. When the loop exits the 'i' goes out of scope and is no longer visible to the rest of the code.
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are new, I corrected the code tags for you (doesn't it look better); please look here for how the tags work.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...please look here for how the tags work.



Thanks Campbell. I did not know this link existed. If I had known, I would have referred to it earlier. It will definitely come in handy!  
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!