The Artima Developer Community
Sponsored Link

Java Answers Forum
Problems with my code

2 replies on 1 page. Most recent reply: Jul 28, 2003 10:29 PM by praveen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Problems with my code Posted: Jul 28, 2003 9:54 AM
Reply to this message Reply
Advertisement
I am trying to use a String variable called answer, I initialize it at the top of my program, but whe I try to use it in a substring, I get an error saying that the variable may not have been initialized.

PLease help

import york.*;
/*
This class plays a game of Hang Man
*/

public class HangManGame
{
public static void main (String[] args)
{
York.println ("Please enter a secret word.");
String secretWord = York.readLine();
char guess;
String answer;
int i = secretWord.length();
int l;
//initializes all variables
York.println("Please choose a letter");
for (l=0; l<i ;l++)
{
York.print(" * ");
}
//prints out astreiks according to how many leters are in secretWord
while (i!=0 && answer!=secretWord)
{
//counts the number of guesses
York.println();
guess = York.readChar();
for (int j=0; j<secretWord.length(); j++ )
{
if (guess==secretWord.charAt(j))
{
York.println();
York.println("You guessed "+guess+ ".");
York.println("You have " +(i)+ " guesses left");
answer = answer.substring(0,j)+guess+answer.substring(j+1);
York.println(answer);
}
}
York.println("Please guess the next letter");
}
}
}


David

Posts: 18
Nickname: malven
Registered: Jul, 2003

Re: Problems with my code Posted: Jul 28, 2003 4:11 PM
Reply to this message Reply
What the compiler is telling you is absolutely right. You have a declaration for answer that looks like this:

String answer;

and then a subsequent assignment that looks like this:

answer = answer.substring(0,j)+guess+answer.substring(j+1);

On the right hand side of the equals sign, the first time through the loop you will be using a reference that never got initialized to a value.

You need to initialize the variable before using it. To do this, think more clearly about that the answer variable is meant to represent. At the beginning, before the user does anything, what should its value be? When they guess a letter correctly, what should it look like then? Try to work out on paper how this variable should change, and then you can see how to modify the code.

Two other quick notes:

1. You have code "while (i!=0 && answer!=secretWord)". This isn't going to work. You need to compare the *contents* of the strings, not the identity (references). Look at the equals() method of String to understand the difference.

2. There are still problems with the overall logic and use of substring() that I answered in the other thread you posted with this code.

praveen

Posts: 3
Nickname: veen
Registered: Jul, 2003

Re: Problems with my code Posted: Jul 28, 2003 10:29 PM
Reply to this message Reply
> What the compiler is telling you is absolutely right. You
> have a declaration for answer that looks like this:
>
> String answer;
>
> and then a subsequent assignment that looks like this:
>
> answer =
> answer.substring(0,j)+guess+answer.substring(j+1);
>
> On the right hand side of the equals sign, the first time
> through the loop you will be using a reference that never
> got initialized to a value.
>
> You need to initialize the variable before using it. To
> do this, think more clearly about that the answer variable
> is meant to represent. At the beginning, before the user
> does anything, what should its value be? When they guess
> a letter correctly, what should it look like then? Try to
> work out on paper how this variable should change, and
> then you can see how to modify the code.
>
> Two other quick notes:
>
> 1. You have code "while (i!=0 && answer!=secretWord)".
> This isn't going to work. You need to compare the
> *contents* of the strings, not the identity (references).
> Look at the equals() method of String to understand the
> e difference.
>
> 2. There are still problems with the overall logic and use
> of substring() that I answered in the other thread you
> posted with this code.

You equate the string to null when u declare it.

Flat View: This topic has 2 replies on 1 page
Topic: I am having a hard time running a jar file. Previous Topic   Next Topic Topic: Base Class, Parent Class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use