• Post Reply Bookmark Topic Watch Topic
  • New Topic

start type  RSS feed

 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a problem with my for loop and i get the error message "illegal start of type"

ArrayList AdvanceTickets = new ArrayList();
for (int i = 0; i < AdvanceTickets.size; i++)
{
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
size is a method.
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right thanks ... um ... what is the import for ArrayList?.. forgot to import it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chris barr:
... um ... what is the import for ArrayList?.. forgot to import it.

If you refer to the API documentation, and click on "ArrayList" in the lower left frame (under "All Classes"), you will see it is in java.util.
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks... ok imported ArrayList but that wasn't the problem.

code/

class Advance extends Ticket
{
int TotaladvPrice = 0;
int advPrice = 0;
int Days = 11;
ArrayList AdvanceTickets = new ArrayList();
(1)for (int i = 0; i < AdvanceTickets.size(); i++)
{
if (Days > 10)
{
advPrice(30);
}

if (Days < 10 && Days > 0)
{
advPrice = 40;
}
}
}(2)

/code

i have tried every that i can think of about for loops and i still get the error "illegal start of type"(1) and "<identifier> expected" (2) for the last curly brace i haven't really tried to fix that one yet though
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to put your code in a method. You can't put statements like the for()-loop in a class declaration.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, you probably want to create a constructor to do this initialization.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might think of it this way: Classes have variables representing what they are (their "state") and methods for what they do.

So when you're describing something a class does, it needs to be inside a method.

(Constructors can be thought of as a special kind of method that gets an object ready for use.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!