• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

start type

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++)
{
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
size is a method.
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right thanks ... um ... what is the import for ArrayList?.. forgot to import it.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, you probably want to create a constructor to do this initialization.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic