What is wrong with the code fragment?

M

Mohammed Mazid

Can anyone please help me here? I run the program with the following
fragment but an error message appears - "flight[] = new Object[6] is
not a statement". If I wrote "Object flight[] = new Object[6]", it
comes with an error "flight[] is already defined". Can anyone please
suggest me corrections?

public Object displayFlight(Object flight[], FlightInformation
flightInfo)
{
flight[] = new Object[6];
String flightNo;
SetupFlightDB flightDB = new SetupFlightDB();
flightInfo = flightDB.flightDetails(flightNo);
flight[0] = flightInfo.getFlightNo();
flight[1] = flightInfo.getFlightDate();
flight[2] = flightInfo.getLocation();
flight[3] = flightInfo.getDepTime();
flight[4] = flightInfo.getArrTime();
flight[5] = flightInfo.getAirline();
}

Much appreciated
 
A

ak

Mohammed Mazid said:
Can anyone please help me here? I run the program with the following
fragment but an error message appears - "flight[] = new Object[6] is
not a statement".

flight = new Object[6];


If I wrote "Object flight[] = new Object[6]", it
comes with an error "flight[] is already defined". Can anyone please
suggest me corrections?

Thats right - flight[] is already defined - it is argument of displayFlight



____________

http://reader.imagero.com the best java image reader.
 
B

Bjorn Abelli

...
Can anyone please help me here? I run the program
with the following fragment but an error message appears

I guess you mean that errors appear already when you compile...

There are at least two errors, but possibly more...
public Object displayFlight
( Object flight[],
FlightInformation flightInfo)
{

The signature of the method raises some questions.

You send in an array of objects, but it's never used, since you try to
*replace* it in the next line with a new array.
flight[] = new Object[6];

....which in turn is misformulated. "flight" itself is the pointer to the
array of objects, which makes the brackets not only redundandt but wrong to
use.

Myself, I always declare arrays the "other way around", since the variable
is a reference to an array, not an array itself, like this:

Object[] variable;

If the intention is to map out the info through the parameter itself, you
shouldn't even try to replace the array, since you loose the reference to it
that way.

On the other hand, the method signature indicates that you will *return* an
Object, which you don't! That's another error. What are you intending to
return?

The next thing in the argumentlist is similar to the above. You send in a
reference to an instance of FlightInformation, which you *also* replace with
another instance:
String flightNo;
SetupFlightDB flightDB = new SetupFlightDB();
flightInfo = flightDB.flightDetails(flightNo);

Besides that, my guess is that this also will go wrong, since you don't have
any value in flightNo, it's null since haven't even initialized it.

Anyway, my final guess is that you're really looking for something like
this:

// Return the array
public Object[] displayFlight(String flightNo)
{
Object[] flight = new Object[6];
SetupFlightDB flightDB = new SetupFlightDB();
FlightInformation flightInfo = flightDB.flightDetails(flightNo);
flight[0] = flightInfo.getFlightNo();
flight[1] = flightInfo.getFlightDate();
flight[2] = flightInfo.getLocation();
flight[3] = flightInfo.getDepTime();
flight[4] = flightInfo.getArrTime();
flight[5] = flightInfo.getAirline();
return flight;
}


Hope this helps

// Bjorn A
 
S

SirThanxALot

Therefore i prefger the use of
Object[] anArray = new Object[3];
to
Object anArray[] = new Object[3];
though they are perfectly equivalent
anArray is just a name

mistakes such as
anArray[] = new Object[6];
result in
"anArray[] = new Object[6]; is not a statement" because
the compiler regards anArray as the declaration of a array of objects from
the class
anArray and then expects a name for the array.
that name is missing here.
Also anArray is not likely to exist.

hope this explains your issue

regards,
srtnxalt

Bjorn Abelli said:
...
Can anyone please help me here? I run the program
with the following fragment but an error message appears

I guess you mean that errors appear already when you compile...

There are at least two errors, but possibly more...
public Object displayFlight
( Object flight[],
FlightInformation flightInfo)
{

The signature of the method raises some questions.

You send in an array of objects, but it's never used, since you try to
*replace* it in the next line with a new array.
flight[] = new Object[6];

...which in turn is misformulated. "flight" itself is the pointer to the
array of objects, which makes the brackets not only redundandt but wrong to
use.

Myself, I always declare arrays the "other way around", since the variable
is a reference to an array, not an array itself, like this:

Object[] variable;

If the intention is to map out the info through the parameter itself, you
shouldn't even try to replace the array, since you loose the reference to it
that way.

On the other hand, the method signature indicates that you will *return* an
Object, which you don't! That's another error. What are you intending to
return?

The next thing in the argumentlist is similar to the above. You send in a
reference to an instance of FlightInformation, which you *also* replace with
another instance:
String flightNo;
SetupFlightDB flightDB = new SetupFlightDB();
flightInfo = flightDB.flightDetails(flightNo);

Besides that, my guess is that this also will go wrong, since you don't have
any value in flightNo, it's null since haven't even initialized it.

Anyway, my final guess is that you're really looking for something like
this:

// Return the array
public Object[] displayFlight(String flightNo)
{
Object[] flight = new Object[6];
SetupFlightDB flightDB = new SetupFlightDB();
FlightInformation flightInfo = flightDB.flightDetails(flightNo);
flight[0] = flightInfo.getFlightNo();
flight[1] = flightInfo.getFlightDate();
flight[2] = flightInfo.getLocation();
flight[3] = flightInfo.getDepTime();
flight[4] = flightInfo.getArrTime();
flight[5] = flightInfo.getAirline();
return flight;
}


Hope this helps

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top