Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By Humphrey Bogart

Thread: Compiling errors - Show me what's wrong

  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Compiling errors - Show me what's wrong

    Hello. I'm working on learning Java. I got this code example out of a book (Head First Java, 2nd ed.), but I get many errors when I try to compile it.

    The code:
    Java Code:
    public class SimpleDotComTestDrive {
    	public static void main (String[] args) {
    		SimpleDotCom dot = new SimpleDotCom();
    		
    		int [] locations = { 2, 3, 4 };
    		dot.setLocationCells (locations);
    		
    		String userGuess = "2";
    		String result = dot.checkYourself(userGuess);
    		String testResult = "failed";
    		if (result.equals("Hit!")) {
    			testResult = "passed";
    		}
    		System.out.println(testResult);
    	}
    
    
    public class SimpleDotCom {
    	int [] locationCells;
    	int numOfHits = 0;
    
    	public void setLocationCells (int[] locs) {
    		locationCells = locs;
    	}
    
    	public String checkYourself(String stringGuess) {
    		int guess = Interger.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;
    	}
    }
    The errors:
    line 37: illegal start of type
    line 37: (identifier) expected
    line 37: illegal start of type
    line 37: (identifier) expected
    line 40: (identifier) expected
    line 40: (identifier) expected
    line 41: illegal start of type
    line 41: ";" expected
    line 44: class, interface, or enum expected

    I don't see the errors in lines 37, 40, or 41. Any help is greatly appreciated.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    If you want us to help you based upon the errors you are receiving, be sure to give us the complete code. There is an error talking about line 44, when your code ends at line 43. How are we supposed to know what line is what line? Also, the errors tell you what is wrong with your code. Read them and think about it.

    You need to indent your code more properly. That would resolve some of your errors. Your curly-brackets are all over the place..

    Integer is spelled Integer, not Interger.

    You can't declare String result inside the first method in the class SimpleDotCom, and expect to be able to use it in elsewhere. Declare it in the beginning of the class as

    Java Code:
    String result;
    and that will solve another error. But overall, just fix your indenting and it will be a lot easier for you to fix your errors.
    Last edited by Zyril; 08-21-2012 at 08:46 PM.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    Hello
    1) My ISP is having an issue today. If I don't respond promptly, it is because I can't get to the web-page.
    2) I made an error in the code. There should be a closing curly brace at line 16. With that correction made, I get the same compilation errors.
    3) I made an error in listing the compilation error messages. The reference to line 44 should be line 43.
    4) I have fixed the mis-spelling of "integer". Thank you for pointing that out.
    5) As I said in my initial post, this code I got verbatim from a book on learning Java. I understand your point about the String result (line 28), and what you say makes sense to me. However, the code I have presented is how the authors wrote it.
    6) I will do some more researching on the error messages. At my current level, the error messages only tell me what line has a problem, but I don't know enough to understand from the message what the problem is. Can you suggest some resources for me in this regard?

    Thanks for your help.

  4. #4
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    Hello. I found and fixed another error: line 30 needs to end in a open curly brace, not a semi-colon.

    Fixing it and will report back.

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    I still think you should read into how you indent and structure your code properly. It will definitely help you while coding, and you will never get errors because stupid things like missing curly brackets.

    About the code itself, are you sure that the code isn't presented with errors that you are supposed to locate and fix? Also, does the code really look the same in the book, no indenting?

    If you want to understand the errors you are receiving, just read them and ask yourself what it could mean.

    "illegal start of type" - Something is fishy about how you start this line, or it could be that you haven't separated something correctly, like for instance you have forgotten a curly bracket...

    Just google the errors and I'm sure you will get hits that explain them perfectly.

    And really, I don't think you would've received more than one error IF you just had used curly brackets and semicolons the correct way. You don't indent your code because the compiler wants you too, because the compiler doesn't give a **** about that. You do it so you see that you aren't missing anything and to make the code easy to read and follow.

  6. #6
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    Hello. The book I got this from presents this code as a (supposedly) example of working code. They even show what output to expect

    Output:
    java SimpleDotComTestDrive
    hit
    passed

    They make this complicated by creating this "TestDrive" classes, that are supposed to test the rest of the code, but they don't explain to the reader (me), how exactly this is all supposed to fit together.

    This is what they show on the page:

    public class SimpleDotComTestDrive {
    public static void main (String[] args) {
    SimpleDotCom dot = new SimpleDotCom();

    int [] locations = { 2, 3, 4 };
    dot.setLocationCells (locations);

    String userGuess = "2";
    String result = dot.checkYourself(userGuess);
    String testResult = "failed";
    if (result.equals("Hit!")) {
    testResult = "passed";
    }
    System.out.println(testResult);
    }
    }

    __________________________________________________ _________

    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;
    }
    }

    Yes, that is a solid line they add above the line that reads "public class SimpleDotCom"

    Having corrected the mistakes in the code identified previously, I now get this error message:

    "Class SimpleDotCom is public and should be delcared in a file named SimpleDotCom.java"

    I'm sorry. My gaps in my knowledge are so large. This must be frustrating (or maybe really funny) to those who know the subject better than I.

  7. #7
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    I still don't think they would print a book with example code that contains errors, and say that it is "working code". You must've missed a brace somewhere when looking through the code.

    The error you are getting is due to the fact that you have two public classes in one class file. The java file should be named exactly the same as the public class that it contains. This said, it doesn't mean that you can have several classes in a java file, though only one public class. Remove the "public" declaration and the compiler wont give you this error.

    ---

    I'm not going to tell you how to learn or that you are doing it the wrong way. I am a newbie at programming myself, and I've just been doing this for about a year, and while studying other subjects I haven't had the time to only practice my programming skills. Though, may I suggest that you perhaps start off by learning something easier than what this book suggest? You say that you are copying the code from the book, and when you do this you get working code that you really don't understand fully. Wouldn't it be better to start of by reading the Java tutorials, or watching some basic tutorials on Youtube to get started with Java at a level suitable for complete beginners?
    Last edited by Zyril; 08-22-2012 at 01:32 AM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Compiling errors - Show me what's wrong

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Compiling errors - Show me what's wrong

    Hello. Following the suggestion of Zyril, I was able to successfully compile and run the code, and it did indeed generate the correct output.

    Thanks to DarrylBurke for your helpful links. I will study them. And thanks to all for your help and patience.
    Rameshwar Soni and quad64bit like this.

Similar Threads

  1. Netbeans IDE Errors while compiling a JSP project
    By pala in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 12-29-2011, 07:35 AM
  2. Replies: 10
    Last Post: 03-16-2011, 05:43 AM
  3. Replies: 16
    Last Post: 08-09-2010, 11:40 PM
  4. Whats Wrong? Errors when compiling
    By software_dude_2009 in forum New To Java
    Replies: 5
    Last Post: 05-12-2009, 12:19 PM
  5. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 09:32 AM

Posting Permissions

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