This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method Hell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Hell" Watch "Method Hell" New topic

Method Hell

simon pieman

Joined: Apr 02, 2005
Posts: 2
Hi - I'm new.
I have a game of hangman that's driving me crazy , mainly the method I called searchWord.

A JButton is pressed (A,B,C...etc) and my method is supposed to;-

1 - send 'tempChar(A,B,C etc...) to the searchWord method.
2 - look through the word to be searched charAt(int) by charAt(int).
3 - if found at index[int], load char[int]output with tempChar.
4 - if not found, load char[int]output with '-'.
5 - Output 'output'.

It may be basic but I've been fiddling/tweaking for 2 days now and I'm about to shoot myself to put me out of my misery!

Here's my 'code'.

//a method to search the 'wordBreakdown'array when a letter is guessed
public void SearchWord(char x,String word2)
char[]output=new char[wordSize];

for(int i=0;i<word2.length();i++)
String st=new String(output);
//output[i]=('-');//THIS WON'T WORK!!!

System.out.println(">"+output+"<");//HERE I GET GOBBLEDYGOOK eg,> [C@1589e56 <


I am willing to marry anyone who can help me get this working!

Yours in hope,

Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Well I think one problem is that you are creating a new (empty) output array each time you call the searchWord method:

So if the word is 'BAR', after the first guess (A), the output will contain '-A-'. Then after the second correct guess (B), the output will contain 'B--', not 'BA-'. To fix this you should make the 'output' array a class member instead of a local variable.

Secondly, this line of code is printing out the array object, which is why you are getting funny output:

It should really print out the String you created earlier:

Finally, you might want to investigate the use of the indexOf methods in the String class.
[ April 28, 2005: Message edited by: Stuart Gray ]
simon pieman

Joined: Apr 02, 2005
Posts: 2
Thanks Stuart - I'm working on it
I agree. Here's the link:
subject: Method Hell
It's not a secret anymore!