Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Head First Java - Array in a Method (call?) Problem

  • 26-03-2011 7:00pm
    #1
    Registered Users, Registered Users 2 Posts: 2,059 ✭✭✭


    Hey,
    I'm having a problem with some code examples from Head First Java, by the O'Reilly people. It's meant to be a simple game like battleship, except without a grid and just a single line.

    I'm using Eclipse.

    I'm having the problem with the "setLocationCells" method (I'm correct in calling it a method I think.) (Also, please excuse me if I'm getting the terminology wrong, I'm still at the very n00b stage of learner.)
    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;
    			}
    		}
    		System.out.println(result);
    		return result;
    	}
    
    }
    

    Here setLocationCells creates a method to create an array called "locs" with the next line causing locationCells array to equal the locs array.

    The for loop that follows goes through the location array cells and if the cell equals a guess, it marks out a "hit."
    public class SimpleDotComGame {
    	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);  //this line is the one that creates the problem
    	boolean isAlive = true;{  //there's a second problem here, or at least I don't understand it.
    	
    	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");
    		}
    	}
    }
    }
    
    

    The line that causes the problem is "theDotCom.setLocationCells(locations);" What it's supposed to be doing here is going to the SimpleDotCom class, and setting the theDotCom object's locs array/locationCells array to the same as the "locations" array from the second example of code. Which should be the randomNum, randomNum+1, randomNum+2 array elements.

    However, I get an error in Eclipse. Which says;
    Multiple markers at this line
    - Syntax error on token "location", VariableDeclaratorId expected after
    this token
    - Syntax error on token(s), misplaced construct(s)

    I don't get what that means. Surely the locations array is a set of numbers. Locations was the name of the array so doesn't the array name send the actual array numbers?





    A second problem with this is that I don't understand why I need the parenthesis after "boolean isAlive = true;" But if I don't put it in I get an error. I'm just declaring an instance variable's value, so why the need for the things you use with conditional stuff?

    The error that comes up if I take the parenthesis out is;
    Multiple markers at this line
    - Watchpoint:SimpleDotComGame [access and modification]
    - isAlive
    - Syntax error on token ";", { expected after this token



    Help would be appreciated. There's a lot going on, and I'm having trouble getting my brain around everything at once. But if I look at how one thing relates to another I can generally see the problem. I just can't manage it here.


Comments

  • Registered Users, Registered Users 2 Posts: 428 ✭✭Joneser


    First thing I notice is that I can't see a constructor method in your SimpleDotCom class unless you have just omitted this to save space.

    In your second block of code, you need to change
    while(isAlive = true)
    
    into
    while(isAlive == true)
    
    "=" is used for assignment, whereas "==" is used for comparison.

    They are the things that immediately jumped out to me, but I'll have a quick look through again and see if I find anything else.


  • Registered Users, Registered Users 2 Posts: 2,059 ✭✭✭Buceph


    In your second block of code, you need to change
    while(isAlive = true)
    
    into
    while(isAlive == true)
    
    "=" is used for assignment, whereas "==" is used for comparison.

    They are the things that immediately jumped out to me, but I'll have a quick look through again and see if I find anything else.

    Done, I got that wrong.
    Joneser wrote: »
    First thing I notice is that I can't see a constructor method in your SimpleDotCom class unless you have just omitted this to save space.

    I don't know what you mean by a "constructor method." This is sample code from a book, and that's everything they put in. There's another class that gets the input if that's what you mean.

    Edit: If you mean something that creates the "int[] locationcells" variable, I thought that the setLocationCells method was getting that from the second class where it was created by the random number * 5 part.
    Second Edit: And it could just be that the book ommited it. It's only page 100 so I'd imagine there's a lot they don't want to broach until I get this down pat.


  • Registered Users, Registered Users 2 Posts: 139 ✭✭seithon


    Every class has a method in it so that when you say "SimpleDotCom myDotCom = new SimpleDotCom();" the constructor method (called public SimpleDotCom() ) is invoked (called) to create a new instance of SimpleDotCom.

    If you need any help feel free to drop me a PM I have the book also :)


  • Registered Users, Registered Users 2 Posts: 36 fermi-paradox


    Buceph wrote: »
    public class SimpleDotComGame {
    	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);  //this line is the one that creates the problem
    	boolean isAlive = true;{  //there's a second problem here, or at least I don't understand it.
    	
    	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");
    		}
    	}
    }
    }
    
    



    Possibly this whole code fragment after the class heading should be your main() method, most of this makes sense as class member initialisations, but not at the point of your error.


  • Registered Users, Registered Users 2 Posts: 2,059 ✭✭✭Buceph


    Possibly this whole code fragment after the class heading should be your main() method, most of this makes sense as class member initialisations, but not at the point of your error.

    It was that, I didn't realise that the book changed this to the main method. They had previously used another bit of code to test a simpler version of this as the main() method, and I didn't notice how it changed when they used this.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭EyeSight


    if i remember correctly, they put all their source code online on their website. it's been a while since i used that book but i remember having a problem like this but their online source code worked


  • Registered Users, Registered Users 2 Posts: 3,739 ✭✭✭Stuxnet


    i find eclipse isnt the best for n00bs in pointing out errors, an ide like bluej might be more suited :D

    from a n00b !


Advertisement