Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Java code error - illegal start of expression.

  1. #1
    Alex25852123 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.
    DarrylBurke likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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?

  4. #4
    Alex25852123 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Java code error - illegal start of expression.

    Quote Originally Posted by Alex25852123 View Post
    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.
    Please understand that the grid variable holds the 2-dimensional array. grid[1][1] represents a char, not the entire array. Since the method is expecting to receive a 2-dimensional array of char, not a single char as you're doing, the error occurs.

  7. #7
    Alex25852123 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Java code error - illegal start of expression.

    Quote Originally Posted by Alex25852123 View Post
    The game method (I replaced all Game by game in my original code) is supposed to show what the user inputs.
    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);
    My grid is 3 by 3
    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.

    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).
    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.


    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
    I meant to paste in the actual assignment instructions that you've been given from your teacher into this forum.

  9. #9
    Alex25852123 is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Java code error - illegal start of expression.

    Quote Originally Posted by Alex25852123 View Post
    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.
    Glad it's helping. Good luck, and please come back if still stuck.

Similar Threads

  1. Error: illegal start of expression
    By iswan in forum JDBC
    Replies: 2
    Last Post: 09-28-2011, 09:29 AM
  2. Error: "Illegal start of expression"
    By mokitooo_1994 in forum New To Java
    Replies: 5
    Last Post: 05-12-2011, 09:42 PM
  3. Illegal start of expression error pls help me!
    By Gayethiri_86 in forum New To Java
    Replies: 12
    Last Post: 05-12-2010, 03:06 PM
  4. Illegal start of expression error
    By lukermsdn111 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 01:31 PM
  5. Servlet Error -illegal start of expression
    By raghu9198 in forum Java Servlet
    Replies: 2
    Last Post: 04-21-2009, 11:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •