user1956201
user1956201

Reputation: 31

JTable Calculator

I want this function to work. This function is called by the game loop to get the selected rows in the table. I dont understand why the error message of: Multiple markers at this line - Type mismatch: cannot convert from int to int[] - Syntax error on token "[", Expression expected after this token

Wondering if anyone could explain what it is that I need to change to make my function getRows work. Thanks for your time and hope to hear back soon.

    public int[] getRows(JTable table) {    
    rows[0] = table.getSelectedRow();
    rowCount = table.getSelectedRowCount() - 1;
    rows[1] = rows[0] + rowCount;
    return rows[];
}

Upvotes: 2

Views: 186

Answers (2)

11684
11684

Reputation: 7517

The error is simple, the explanation isn't.

What you did wrong: return rows[];. It should be return rows;.

The explanation:

When you want to return an array you should not add the brackets. If you would want to return one item of an array you should write return array[index];. What the compiler thinks in your case is that you are trying to return one int from the array rows but forgot the index, that's a syntax error. The type mismatch is because you said you would return an int[] (and the compiler thought you are trying to return an int, remember?) in the method header.

Upvotes: 5

Alya'a Gamal
Alya'a Gamal

Reputation: 5638

public int[] getRows(JTable table) {    
    rows[0] = table.getSelectedRow();
    rowCount = table.getSelectedRowCount() - 1;
    rows[1] = rows[0] + rowCount;
    return rows; //////////without brackets
}

Upvotes: 0

Related Questions