CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2011
    Posts
    22

    [RESOLVED] Cannot find symbol class

    I am new to java. I am trying a simple game with three classes SimpleDotComGame, SimpleDotCom, GameHelper in three different .java files all put together in a directory dotGame. SimpleDotComGame.java has the main method. I have declared package name as

    Code:
    package dotGame
    in each of these files. SimpleDotCom.java and GameHelper.java compile without any errors. But when I run SimpleDotComGame.java I get the following error:

    Code:
    cannot find symbol
    symbol: class GameHelper
    Please help me with the same.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Cannot find symbol class

    SimpleDotComGame.java is looking for a class called GameHelper when the class is actually called dotGame.GameHelper which is why it can't find it.

    Have you recompiled SimpleDotComGame.java after adding the package name to the other classes.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 2011
    Posts
    22

    Re: Cannot find symbol class

    Quote Originally Posted by keang View Post
    Have you recompiled SimpleDotComGame.java after adding the package name to the other classes.
    Yes I have added the package to other classes and then compiled them too. The error still persists.

  4. #4
    Join Date
    Aug 2011
    Posts
    22

    Re: Cannot find symbol class

    Hey, I got the solution. The code started working fine when I removed
    Code:
    package dotGame;
    from all the three classess!

    But I didn't get the reason why!

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Cannot find symbol class

    It works without the package name because classes without a package name are all put in the default package and as long as they are all in the same directory they are all visible to each other.

    It will also work with the package name if you set it up correctly. Can you post all the code and/or show how you are compiling the classes and running the app and also any warning or error messages that appear for both compile and runtime.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Aug 2011
    Posts
    22

    Re: Cannot find symbol class

    Quote Originally Posted by keang View Post
    It will also work with the package name if you set it up correctly. Can you post all the code and/or show how you are compiling the classes and running the app
    Code:
    //code for SimpleDotCom
    package dotGame;
    public class SimpleDotCom {
    	int [] locationCells ;
    	int numOfHits = 0;
    
    	public void setLocationCells(int [] locs) {
    			locationCells = locs;
    	}
    	
    	public String checkYourself(String stringGuess) {
    		int guess = Integer.parseInt(stringGuess);
    		String result = "miss";
    		for (int cell : locationCells) {
    			if (guess == cell)
    			{
    				result = "hit";
    				numOfHits++;
    				break;				
    			}
    		}
    
    		if (numOfHits == locationCells.length)
    		{
    			result = "kill";
    		}
    
    			System.out.println(result);
    			return result;
    	}
    }
    Code:
    //Code for GameHelper.java
    package dotGame;
    import java.io.* ;
    public class GameHelper 
    {
    	public String getUserInput(String prompt) {
    		String inputLine = null;
    		System.out.print (prompt + " ");
    		try
    		{
    			BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    			inputLine = is.readLine();
    			if (inputLine.length() == 0)
    			{
    				return null;
    			}
    		}
    		catch (IOException e)
    		{
    			System.out.println("IOException: " +e);
    		}
    		return inputLine;
    	}
    }
    Code:
    //Code for SimpleDotComGame.java
    package dotGame;
    public class SimpleDotComGame
    {
    	public static void main(String[] args) 
    	{
    		int numOfGuesses = 0;
    		GameHelper helper = new GameHelper();
    		SimpleDotCom theDotCom = new SimpleDotCom();
    		int randomNum = (int)(Math.random() * 5);
    
    		int [] locations = {randomNum, randomNum+1, randomNum+2};
    		theDotCom.setLocationCells(locations);
    		boolean isAlive = true;
    
    		while (isAlive == true)
    		{
    			String guess = helper.getUserInput("Enter a number");
    			String result = theDotCom.checkYourself(guess);
    			numOfGuesses++;
    
    			if (result.equals("kill"))
    			{
    				isAlive = false;
    				System.out.println("You took " + numOfGuesses + " guesses");
    			}
    		}
    	}
    }
    Quote Originally Posted by keang View Post
    and also any warning or error messages that appear for both compile and runtime.
    Errors:
    Code:
    E:\dotGame>javac SimpleDotCom.java
    
    E:\dotGame>javac GameHelper.java
    
    E:\dotGame>javac SimpleDotComGame.java
    SimpleDotComGame.java:8: cannot find symbol
    symbol  : class GameHelper
    location: class dotGame.SimpleDotComGame
                    GameHelper helper = new GameHelper();
                    ^
    SimpleDotComGame.java:8: cannot find symbol
    symbol  : class GameHelper
    location: class dotGame.SimpleDotComGame
                    GameHelper helper = new GameHelper();
                                            ^
    SimpleDotComGame.java:9: cannot find symbol
    symbol  : class SimpleDotCom
    location: class dotGame.SimpleDotComGame
                    SimpleDotCom theDotCom = new SimpleDotCom();
                    ^
    SimpleDotComGame.java:9: cannot find symbol
    symbol  : class SimpleDotCom
    location: class dotGame.SimpleDotComGame
                    SimpleDotCom theDotCom = new SimpleDotCom();
                                                 ^
    4 errors
    I have saved all the files in a folder dotGame in E:\

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Cannot find symbol class

    When code is in a package you need to compile it from the root of the package ie:
    Code:
    E:>javac dotGame\SimpleDotComGame.java
    If your source had been in the directory E:\SRC\dotGame then you would do
    Code:
    E:\SRC>javac dotGame\SimpleDotComGame.java
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Aug 2011
    Posts
    22

    Re: Cannot find symbol class

    It's compiling fine now after your reply @keang. How do I run it btw?

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Cannot find symbol class

    Code:
    E:>java dotGame.SimpleDotComGame
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Aug 2011
    Posts
    22

    Re: Cannot find symbol class

    Hey thanks a lot!!! It's working!!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured