Results 1 to 7 of 7
  1. #1
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default Vector Errors...

    I'm trying to create a Snake for my game using the vector class. using Vector will make it easier to make the snake grow when it eats something. However, I'm having issues trying to create the snake. Here is the code i have so far followed by the errors i receive. Could someone please help me with these errors?
    Thanks in advance...
    Java Code:
    import java.util.Vector;
    
    public class Snake {
    
    	
    	Vector Snake = new Vector();
    	int cap = 10;
    	int capInc = 5;
    	char body = '~';
    	Snake(cap initialCapacity, capInc capacityIncrement);
    	{
    		Snake.add("+");//head of Snake.
    		Snake.add("~");//body of snake.
    		Snake.add("~");//body of snake.
    		Snake.add("~");//body of snake.
    		Snake.add(2, body);//add body of snake to index 2. (Use this in loops later on, just testing now).
    		
    	}
    }
    Java Code:
    Snake.java:10: cannot find symbol
    symbol  : class cap
    location: class Snake
    	Snake(cap initialCapacity, capInc capacityIncrement);
    	      ^
    Snake.java:10: cannot find symbol
    symbol  : class capInc
    location: class Snake
    	Snake(cap initialCapacity, capInc capacityIncrement);
    	                           ^
    Snake.java:10: missing method body, or declare abstract
    	Snake(cap initialCapacity, capInc capacityIncrement);
    	^
    Note: Snake.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    3 errors

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    cap and capInc are variables not types, and a constructor declaration needs types.

  3. #3
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Vector

    public Vector(int initialCapacity,
    int capacityIncrement)

    Constructs an empty vector with the specified initial capacity and capacity increment.

    Parameters:
    initialCapacity - the initial capacity of the vector.
    capacityIncrement - the amount by which the capacity is increased when the vector overflows.
    Throws:
    IllegalArgumentException - if the specified initial capacity is negative
    Thats from the java api, where it says int i created an int variable for it to do the job. But obviously thats wrong. What do i need to do?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Go through the basic tutorials again. Right now you are unable to even call a method or use constructors.

    when you see the definition
    Java Code:
    public Vector(int initialCapacity, int capacityIncrement)
    it means you call it like this
    Java Code:
    int a = 1;
    int b = 2;
    Vector c = new Vector(a, b);

  5. #5
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    I've sorted that out now.
    Java Code:
    Vector Snake = new vector(10, 5);
    As simple as that, however, now I've created my Vector it wont let me add anything to it. As far as i can see, after research on-line and through a book there is nothing wrong with this code, but for some reason i get these errors with it.

    Java Code:
    import java.util.Vector;
    
    public class Snake {
    	Vector Snake = new Vector(10, 5);
    	Snake.add("+");
    }
    Java Code:
    Snake.java:5: <identifier> expected
    	Snake.add("+");
    	         ^
    Snake.java:5: illegal start of type
    	Snake.add("+");
    	          ^
    2 errors

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    You can't perform actions from the class defintion level. Those actions (the add calls) must be contained within a method, constructor, or static block, whichever is applicable.

    Are you taking a class? Or are you simply stumbling through this on your own? If the second go here:
    The Java™ Tutorials
    and work through the tutorials one after the other starting with the first one.

    If the first (i.e. taking a class) start paying attention to the lectures.

  7. #7
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    You can't perform actions from the class defintion level. Those actions (the add calls) must be contained within a method, constructor, or static block, whichever is applicable.

    Are you taking a class? Or are you simply stumbling through this on your own? If the second go here:
    The Java™ Tutorials
    and work through the tutorials one after the other starting with the first one.

    If the first (i.e. taking a class) start paying attention to the lectures.
    ha ha, i do take class but he only briefly skipped over Vectors and now I'm using them in my project. I pay attention 95% of the time and make notes plus i use a Dictaphone to in case i miss anything. But ill just have a quick look at these tutorials anyway for now. I'll post back if i get stuck.

Similar Threads

  1. Getting errors for some reason
    By Swarvy in forum New To Java
    Replies: 7
    Last Post: 09-30-2008, 02:45 PM
  2. makeButton errors
    By ljk8950 in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2008, 01:10 AM
  3. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM
  4. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 09:32 AM
  5. Errors in constructor
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 07-01-2007, 05:35 PM

Posting Permissions

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