Results 1 to 10 of 10
- 10-20-2012, 04:07 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Java code error - illegal start of expression.
Java Code:import java.util.Scanner; public class TicTacToe{ public static void Game(char[][] grid){ System.out.println(" " + grid[1][1] + "| " + grid[1][2] + "| " + grid[1][3]); System.out.println(" " + grid[1][1] + "| " + grid[1][2] + "| " + grid[1][3]); System.out.println(" " + grid[1][1] + "| " + grid[1][2] + "| " + grid[1][3]); } public static void main(String[] args){ Scanner keyboard = new Scanner(System.in); char[][] grid = new char[4][4]; System.out.println("Enter the row number:"); int row = keyboard.nextInt(); System.out.println("Enter the column number:"); int col = keyboard.nextInt(); for (int rows = 1; rows < 4; rows++) for (int cols = 1; cols < 4; cols++) grid[rows][cols] = 'O'; if (row == 1){ if (col == 1) System.out.println(Game(grid[1][1])); // This is where the error is and it continues : else if (col == 2) System.out.println(Game(grid[1][2]));// its here too else if (col == 3) System.out.println(Game(grid[1][3])); } else if (row == 2){ if (col == 1) System.out.println(Game(grid[2][1])); else if (col == 2) System.out.println(Game(grid[2][2])); else if (col == 3) System.out.println(Game(grid[2][3])); } else if (row == 3){ if (col == 1) System.out.println(Game(grid[3][1])); else if (col == 2) System.out.println(Game(grid[3][2])); else if (col == 3) System.out.println(Game(grid[3][3])); } } }
Compiling error:
Java Code:-------------------Configuration: <Default>-------------------- G:\Java Saves File\TicTacToe.java:27: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[1][1])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:30: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[1][2])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:33: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[1][3])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:38: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[2][1])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:41: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[2][2])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:44: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[2][3])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:49: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[3][1])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:52: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[3][2])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion G:\Java Saves File\TicTacToe.java:55: error: method Game in class TicTacToe cannot be applied to given types; System.out.println(Game(grid[3][3])); ^ required: char[][] found: char reason: actual argument char cannot be converted to char[][] by method invocation conversion 9 errors Process completed.
Last edited by Alex25852123; 10-20-2012 at 04:32 AM. Reason: code tags added, ASAP removed
-
Re: Java code error - illegal start of expression.
Moderator edit: code tags added to improve readability of code and error messages and "Please help ASAP" removed to improve chances of getting help.
-
Re: Java code error - illegal start of expression.
Your Game method (which should be re-named to game) expects an array of char to be passed into it, and you're only passing in a char. But in order to help us better understand what you *should* be doing, can you tell us what your assignment instructs you to do?
- 10-20-2012, 04:24 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Java code error - illegal start of expression.
My assignment is to build a Tic Tac Toe game using the 2D array. As you can see i have not fully completed the program, im just in the beginning. The error pops up when i get the user input to store it in the method Game so it prints out with a "O" in the screen. I have put and array in the method which is a char : Game(grid[1][1]) , as shown in the code. The grid is my 2D array.
Last edited by Alex25852123; 10-20-2012 at 04:29 AM.
-
Re: Java code error - illegal start of expression.
So the Game method (again you should re-name it) is supposed to store the data entered? It really doesn't do anything of the kind, and in fact all it does is display the values held in the 2-dimensional array that is passed into it. I'm sorry, but I'm quite confused. Can you post your actual assignment verbatim?
-
Re: Java code error - illegal start of expression.
- 10-20-2012, 04:44 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Java code error - illegal start of expression.
The game method (I replaced all Game by game in my original code) is supposed to show what the user inputs. My grid is 3 by 3 and the user can choose row that is between 1 and three, he also have to choose a column that is between 1 and three (The two input i am getting form the user are the row and column). After that my code is supposed to print out the 'O' in the right place the user wants it to. I'm sorry that I don't get what you mean by an "assignment verbatim", my task is only to do a multilayer tic tac toe game and im still in the very beginning, i know it does not look like that at all. I do not get what you mean by "grid[1][1] represents a char, not the entire array", am i doing it wrong? if so how am i suppose to give the method a 2 dimensional array that represents a char?
Last edited by Alex25852123; 10-20-2012 at 04:51 AM.
-
Re: Java code error - illegal start of expression.
OK, if the game method is just for display, then pass it the entire array, the grid variable, not a single item in the array:
Java Code:game(grid);
No, actually your grid is 4 by 4. Remember that Java arrays are 0 based. If this were my program I'd use a char[3][3] array, and would work with row and column values between 0 and 2.My grid is 3 by 3
You can still do that with my suggestion above, just decrement the value input by the user by 1. Your code does funny things though after getting the user's input including setting all the items in your two-dimensional array to 'O' by using a for loop when it probably shouldn't be using one.and the user can choose row that is between 1 and three, he also have to choose a column that is between 1 and three (The two input i am getting form the user are the row and column).
I meant to paste in the actual assignment instructions that you've been given from your teacher into this forum.I'm sorry that I don't get what you mean by an "assignment verbatim", my task is only to do a multilayer tic tac toe game and im still in the very beginning, i know it does not look like that at all. I mentioned on the top
- 10-20-2012, 05:12 AM #9
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Java code error - illegal start of expression.
Thank you for the help, im slowly starting to understand what you meant, I probably don't know about methods a lot. But the advice you gave me makes more sense now. I'll fix the whole code soon.
-
Re: Java code error - illegal start of expression.
Similar Threads
-
Error: illegal start of expression
By iswan in forum JDBCReplies: 2Last Post: 09-28-2011, 09:29 AM -
Error: "Illegal start of expression"
By mokitooo_1994 in forum New To JavaReplies: 5Last Post: 05-12-2011, 09:42 PM -
Illegal start of expression error pls help me!
By Gayethiri_86 in forum New To JavaReplies: 12Last Post: 05-12-2010, 03:06 PM -
Illegal start of expression error
By lukermsdn111 in forum New To JavaReplies: 9Last Post: 03-22-2010, 01:31 PM -
Servlet Error -illegal start of expression
By raghu9198 in forum Java ServletReplies: 2Last Post: 04-21-2009, 11:12 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks