user6139300
user6139300

Reputation:

Cannot return value from another method

import java.util.Scanner;
public class testing{

public static int getError(int inputnum, int[][] grid, int errorCode){      

    if(inputnum <0 || inputnum > grid[0].length){

        System.out.println("Range of column should be 0 to 6!");
        //change errorCode to 1 to go back to the begining of while loop
        errorCode = 1;
    }

    if(grid[5][inputnum] != 0){

        System.out.println("Column " + inputnum + " is full");
        errorCode = 1;
    }

    return errorCode;
    }

public static void main(String[] args)
{
    int player = 1;
    int errorCode = 0;
    int[][] grid = CreateGrid();
    boolean loop = true;

    drawGrid(grid);     
    Scanner input = new Scanner(System.in);


    while(loop){


    System.out.print("Player " + player + " type a column <0-6> or 9 to quit 
    current game:");

    int inputnum = input.nextInt();         
    getError(inputnum, grid, errorCode);
    errorCode = getError(errorCode);
    if (errorCode == 1)
        continue;
    }
}

Not sure why I cannot pass the errorcode from getError method to the main method. It says:

fourinaline_reference_edited.java:140: error: cannot find symbol errorCode = getError(errorCode2); ^ symbol: variable errorCode2 location: class fourinaline_reference_edited 1 error

How can I solve it?

Upvotes: 0

Views: 116

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

You don't pass the right type to your method :

errorCode = getError(errorCode);

your method should take int, array of int[][], and int, not just int:

getError(int inputnum, int[][] grid, int errorCode)

Upvotes: 4

SF23
SF23

Reputation: 184

Why don't you do

errorCode = getError(inputnum,grid,errorCode);

directly?

Upvotes: 1

Related Questions