8 Replies - 470 Views - Last Post: 14 May 2013 - 08:22 PM Rate Topic: -----

#1 Crewd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 13

getting illegal start of expression errors with collections

Posted 13 May 2013 - 04:43 PM

//Create a new class called Gerbil with an int gerbilNumber that's initialized
//in the constructor. Give it a method called hop() that displays which gerbil number this is,
//and that it's hopping. Create an ArrayList and add Gerbil objects to the List. Now
//use the get() method to move through the List and call hop() for each Gerbil.

class Gerbil{
Gerbil(){
private static int counter;
private final int gerbilN = counter++;
public int gerbilN() { return gerbilN };
}
public void hop(){
	System.out.println("Gerbil number is", + GerbilN + "and it's hopping.");
	}
}
public class GerbilGenerator{
public static void main(String[] args){
ArrayList<Gerbil> gerbils = new ArrayList <Gerbil>();
for(int i = 0; i < 5; i++)
gerbils.add (new Gerbil());

for (int i = 0; i < gerbils.size(); i++)
gerbils.get(i).gerbilN();

for (Gerbil g : gerbils)
g.hop();
	}
}




I'm getting illegal start of operation, ; expected, and not a statement for the things inside the constructor.
It's based off an example in the book and I just changed it a little, not sure what I'm doing wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: getting illegal start of expression errors with collections

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: getting illegal start of expression errors with collections

Posted 13 May 2013 - 05:11 PM

Methods, even constructors, can't contain methods.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: getting illegal start of expression errors with collections

Posted 13 May 2013 - 07:24 PM

And Greg's conclusions are easy tosee if the code is correctly indented

class Gerbil{
	Gerbil(){
		private static int counter;
		private final int gerbilN = counter++;
		public int gerbilN() { return gerbilN };
	}
	public void hop(){
		System.out.println("Gerbil number is", + GerbilN + "and it's hopping.");
	}
}
public class GerbilGenerator{
	public static void main(String[] args){
		ArrayList<Gerbil> gerbils = new ArrayList <Gerbil>();
		for(int i = 0; i < 5; i++)
			gerbils.add (new Gerbil());

		for (int i = 0; i < gerbils.size(); i++)
			gerbils.get(i).gerbilN();

		for (Gerbil g : gerbils)
			g.hop();
	}
}



and a single .java file an only hold one single public class so
GerbilGenerator should become private or put into its own GerbilGenerator.java file
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: getting illegal start of expression errors with collections

Posted 13 May 2013 - 09:04 PM

Quote

and a single .java file an only hold one single public class so
GerbilGenerator should become private or put into its own GerbilGenerator.java file

There is only a single top-level public class, so both Gerbil and GerbilGenerator can exist in the same file called GerbilGenerator.java as written. Well, as written but with the other stuff cleaned up.
Was This Post Helpful? 0
  • +
  • -

#5 Crewd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 13

Re: getting illegal start of expression errors with collections

Posted 13 May 2013 - 09:34 PM

Thanks. I took the methods out the constructor, but and just left them in the Gerbil class, but I'm still getting the exact same error messages. I'm going to work on it and figure out what's going on.
Was This Post Helpful? 0
  • +
  • -

#6 Crewd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 13

Re: getting illegal start of expression errors with collections

Posted 13 May 2013 - 10:17 PM

Pretty sure the problem is that I'm declaring variables inside the constructor. I didn't look at it as a a method until you told me, and since I wouldn't do that with any other method, I probably shouldn't be doing it with the constructor either.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: getting illegal start of expression errors with collections

Posted 14 May 2013 - 01:02 AM

Post your updated code and current error messages.

Taking the single method ouf of the Gerbil constructor and fixing a semi-colon in that method should have solved the "Illegal start of expression" error. There were other minor fixes, including a typo and variable tweaks needed to clean it up, but nothing major. It's a cute little demo of some simple but important concepts when it's done.

Declaring variables inside a method, even the constructor, is okay if the variables are only needed to be local to that method. In this case, you're right, the variables are needed elsewhere, so declaring them outside the constructor is best.

This post has been edited by GregBrannon: 14 May 2013 - 01:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 Crewd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-April 13

Re: getting illegal start of expression errors with collections

Posted 14 May 2013 - 06:02 PM

//Create a new class called Gerbil with an int gerbilNumber that's initialized
//in the constructor. Give it a method called hop() that displays which gerbil number this is,
//and that it's hopping. Create an ArrayList and add Gerbil objects to the List. Now
//use the get() method to move through the List and call hop() for each Gerbil.
import java.util.*;
class Gerbil{

private final int gerbiln;


Gerbil(gerbilN) { this.gerbiln = gerbiln; }
public void hop(){System.out.println("Gerbil."+gerbiln); }

}
public class GerbilGenerator{
public static void main(String[] args){
ArrayList<Gerbil> gerbils = new ArrayList<Gerbil>();
for(int i = 0; i < 5; i++)
gerbils.add(new Gerbil(i));

for (int i = 0; i < gerbils.size(); i++)
gerbils.get(i).hop();
	}
}


This works. My only question is, why does the method parameter for the Gerbil constructor have to be named gerbiln in order for the gerbiln variable to get initialized? if they're not the same, I get might not have been initialized errors.

and yeah I didn't put the "and it's hopping in there." but at that point I was just trying to get the numbers to increment.

This post has been edited by Crewd: 14 May 2013 - 06:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Re: getting illegal start of expression errors with collections

Posted 14 May 2013 - 08:22 PM

first off this is not compiling but the reason its not compiling is the answer to your question.

    Gerbil(int gerbiln) 
    {
        this.gerbiln = gerbiln; 
    }


what this code is saying is that a caller will input a number into the same field name as an already used variable and using the this. command you are saying that gerbiln will be gerbiln this is called shadowing this keyword


and by the way this code:

 private final int gerbiln;


should really be like this
 private final int GERBLIN;


the reason is that when you create a final variable common practice is to uppercase the variable name and also please indent your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1