Results 1 to 7 of 7
Thread: Vector Errors...
- 11-12-2008, 01:30 PM #1
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
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
- 11-12-2008, 01:35 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
cap and capInc are variables not types, and a constructor declaration needs types.
- 11-12-2008, 01:49 PM #3
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
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?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
- 11-12-2008, 02:18 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Go through the basic tutorials again. Right now you are unable to even call a method or use constructors.
when you see the definition
it means you call it like thisJava Code:public Vector(int initialCapacity, int capacityIncrement)
Java Code:int a = 1; int b = 2; Vector c = new Vector(a, b);
- 11-12-2008, 02:32 PM #5
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
I've sorted that out now.
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:Vector Snake = new vector(10, 5);
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
- 11-12-2008, 02:44 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-12-2008, 02:49 PM #7
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
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
-
Getting errors for some reason
By Swarvy in forum New To JavaReplies: 7Last Post: 09-30-2008, 02:45 PM -
makeButton errors
By ljk8950 in forum AWT / SwingReplies: 12Last Post: 08-10-2008, 01:10 AM -
help with these errors
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-16-2008, 04:55 PM -
I have 3 errors after compiling
By coco in forum JDBCReplies: 2Last Post: 10-18-2007, 09:32 AM -
Errors in constructor
By ai_2007 in forum Advanced JavaReplies: 0Last Post: 07-01-2007, 05:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks