BuySomeChips
BuySomeChips

Reputation: 41

How to return a string?

import java.util.*;
public class HangManP5 
{
public static void main(String[] args) 
{
int attempts = 10;
int wordLength;
boolean solved;
Scanner k = new Scanner(System.in);
System.out.println("Hey, what's your name?");
String name = k.nextLine();
System.out.println(name+ ", hey! This is a hangman game!\n");
RandomWord(word);
int len = word.length();
char[] temp = new char[len];
for(int i = 0; i < temp.length; i++)
{
    temp[i] = '*';
}
System.out.print("\n");
System.out.print("Word to date: ");
while (attempts <= 10 && attempts > 0)
{
    System.out.println("\nAttempts left: " + attempts);
    System.out.print("Enter letter: ");
    String test = k.next();
    if(test.length() != 1) 
    {
        System.out.println("Please enter 1 character");
        continue;
    }
    char testChar = test.charAt(0);
    int foundPos = -2;
    int foundCount = 0;
    while((foundPos = word.indexOf(testChar, foundPos + 1)) != -1)
    {
        temp[foundPos] = testChar;
        foundCount++;
        len--;
    }
    if(foundCount == 0)
    {
        System.out.println("Sorry, didn't find any matches for " + test);
    }
    else
    {
        System.out.println("Found " + foundCount + " matches for " + test);
    }

    for(int i = 0; i < temp.length; i++)
    {
        System.out.print(temp[i]);
    }
    System.out.println();

    if(len == 0)
    {
        break; //Solved!
    }
    attempts--;
 }

if(len == 0)
{
    System.out.println("\n---------------------------");
    System.out.println("Solved!");
}
else
{
    System.out.println("\n---------------------------");
    System.out.println("Sorry you didn't find the mystery word!");
    System.out.println("It was \"" + word + "\"");
}
}
public static String RandomWord(String word)
{
//List of words
Random r = new Random();
int a = 1 + r.nextInt(5);
if(a == 1)
{
    word=("Peace");
}
if(a == 2)
{
    word=("Nuts");
}
if(a == 3)
{
    word=("Cool");
}
if(a == 4)
{
    word=("Fizz");
}
if(a == 5)
{
    word=("Awesome");
}
 return (word);
 }
 }

Ok, so this is my code for a hangman game, the only thing I have left to do is to get my program to randomize one of the words, which it should do in the method successfully. But the only problem I'm having is getting the String variable "word" to go back to the main class (there are errors underlining all the "word" variables in the main class).

If I could get help with either this or another way to produce a random word from a list, that would be amazing.

Upvotes: 2

Views: 132

Answers (2)

Jean Logeart
Jean Logeart

Reputation: 53829

In java, parameters are passed by value and not by reference. Therefore, you cannot change the reference of a parameter.

In your case, you need to do:

public static String getRandomWord() {
    switch(new Random().nextInt(5)) {
        case 0:
            return "Peace";
        case 1:
            return "Nuts";
        // ...
        default:
            throw new IllegalStateException("Something went wrong!");
    }
}

And in main:

// ...
String word = getRandomWord();
int len = word.length(); 
// ...

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

You can't modify the caller's reference.

RandomWord(word);

needs to be something like

word = RandomWord(word);

Also, by convention, Java methods start with a lower case letter. And, you could return the word without passing one in as an argument and I suggest you save your Random reference and use an array like

private static Random rand = new Random();
public static String randomWord() {
    String[] words = { "Peace", "Nuts", "Cool", "Fizz", "Awesome" };
    return words[rand.nextInt(words.length)];
}

And then call it like

word = randomWord();

Upvotes: 2

Related Questions