0

So i'm trying to call a method displayBoard in java that displays the boardgame once the user enters in a number initially in the main method. I'm unable to call this method. Does anyone see where i'm going wrong or how do i fix this? Thanks.

public static void displayBoard(int [] board, boolean showItem)
{
    System.out.println();
    System.out.print("_");
    for (int val : board){
        switch(codes){
        case 2:
        System.out.print("x");
        break;
        case 3:
        System.out.print(" ");
        break;
        case 4:
        System.out.print(showItem ? "Y" : " ");
        break;
    }
    System.out.print("_");
} //for
System.out.println();
System.out.println();
}//display

public static void main (String [] args)
{
    int guess = 0;
    int userInput = promptForInt("Length Of board");
    int [] numbers = new int[userInput];
    int randomlocs = new Random().nextInt(userInput);
    int val;
    int display = displayBoard(board [], boolean showItem) // doesnt work?
    boolean showItem = false;


    while(! showItem)
    {
        val = promptForInt("Try Again!");
        if(guess == randomlocation)
        {
            System.out.println("Found it!");
            showItem = true;
        }
        else if(guess != randomlocs)
        System.out.print(val);
    }
}
0

1 Answer 1

2

Problem

You must pass values to the method call. Right now, you are passing declarations to the method, which isn't proper Java syntax

How To Fix

First, declare your showItem boolean before you call the method so you have a boolean to pass to the method. It should look like this:

boolean showItem = false;
int display = displayBoard(numbers, showItem)

This will pass the vakues stored in your numbers and showItem variables. We know the values stored in these specific variables (numbers and showItem) should be passed in due to the method's parameter names.

The statements leading up to that method call should look like this:

int userInput = promptForInt("Length Of board");
int [] numbers = new int[userInput];
boolean showItem = false;
int display = displayBoard(board [], boolean showItem);

int randomlocs = new Random().nextInt(userInput); //since this isn't used before the method call, it should be declared below it
int guess = 0; //same with this
int val; //and this
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer, I understand where i'm going wrong now. I'm trying this at the moment and its saying "incompatable types".
@Zynk That is because displayBoard is a void method, which does not return a value. It should either be an int method, which returns an integer, or you shouldn't be initializing a variable with it, like you are with int display = displayBoard(int[], boolean). The way I see it, you don't need the int display variable
Ah i see, it doesnt look like i need it then but when I run the execute the program the board doesn't display when the user is promoted for a value in promptForInt which is why I thought I needed it. Thank you for your help! :)
@Zynk No problem! :) Don't forget to mark as accepted answer if it fixed your problem, to let others know how to fix it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.