Results 1 to 3 of 3
  1. #1
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default newbie Game question

    Im trying to run this code and all seems fine to me I think it may have an error somewhere that I don't see thats causing a string of errors...or it ...idk. Bless me with your knowledge oh mighty java gurus of the interweb. Any help is greatly appreciated :)

    Java Code:
    public class BattleShipsTestDrive {
    
    	public static void main (String[] args) {
    
    		BattleShip megaship = new BattleShip();
    
    		int[] locations = {2,3,4};
    		
    		megaship.setLocationCells(locations);
    	
    		String userGuess = "2";
    
    		String result = megaship.checkYourself(userguess);
    	
    	}
    
    }
    
    
    public class BattleShip {
    
    	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;
    		}
    
    }
    This is the error(s) that command prompt gives me.

    Java Code:
    BattleShip.java:37: illegal start of type
                    for(int cell : locationCells) {
                    ^
    BattleShip.java:37: ')' expected
                    for(int cell : locationCells) {
                                ^
    BattleShip.java:37: <identifier> expected
                    for(int cell : locationCells) {
                                                ^
    BattleShip.java:50: illegal start of type
                    if (numOfHits == locationCells.length) {
                    ^
    BattleShip.java:50: <identifier> expected
                    if (numOfHits == locationCells.length) {
                                 ^
    BattleShip.java:50: ';' expected
                    if (numOfHits == locationCells.length) {
                                    ^
    BattleShip.java:50: illegal start of type
                    if (numOfHits == locationCells.length) {
                                                  ^
    BattleShip.java:50: ';' expected
                    if (numOfHits == locationCells.length) {
                                                         ^
    BattleShip.java:55: <identifier> expected
                    System.out.println(result);
                                      ^
    BattleShip.java:55: <identifier> expected
                    System.out.println(result);
                                             ^
    BattleShip.java:57: illegal start of type
                            return result;
                            ^
    BattleShip.java:57: ';' expected
                            return result;
                                  ^
    BattleShip.java:60: class, interface, or enum expected
    }
    ^
    13 errors
    I've tinkered with it and I'm stumped thanks in advance! :)

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

    Default

    You seem to have method calls dangling in the middle of your class, not in a constructor or method block. You can fix that by putting the method calls in a method where they belong.

  3. #3
    MrNiceGuy is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    haha wow that was a simple fix funny its always the simple things you overlook >.< thanks alot! :)

Similar Threads

  1. Newbie Loop Question
    By MrNiceGuy in forum New To Java
    Replies: 4
    Last Post: 11-14-2010, 02:27 PM
  2. Newbie question
    By MrNiceGuy in forum New To Java
    Replies: 8
    Last Post: 11-12-2010, 05:29 AM
  3. Replies: 8
    Last Post: 09-23-2010, 12:48 AM
  4. newbie question
    By ronguilmet in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 02:37 AM
  5. Newbie question; Vectors
    By Kern in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 06:59 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
  •