Gibberish output is display when returning an array

M

Mohammed Mazid

I changed my code as I initially used an Object arrays, I decided to
use String array instead as I can convert from a date/time to a
string.

However, when I compiled and run, it displayed a nonsense output.
Something in the nature of "java.lang.String@28934".

Here is my latest code fragment.

//This will be passed to the method displayFlights
String flight1 = "BA123";

String flightArray[] = new String[6];

for (int i = 0; i < flightArray.length; i++)
{
out.println("<td>" );
out.println(displayFlight(flight1));
out.println("</td>" );
out.println("<br>");
}
}

// Return an array of flight details
public String[] displayFlight(String flightNo)
{
String[] flight = new String[6];
SetupFlightDB flightDB = new SetupFlightDB();
FlightInformation flightInfo = flightDB.flightDetails(flightNo);

flight[0] = flightInfo.getFlightNo();

java.sql.Date flightDate = flightInfo.getFlightDate();
String dateString = String.valueOf(flightDate);
flight[1] = dateString;

flight[2] = flightInfo.getDestination();

java.sql.Time depTime = flightInfo.getDepTime();
String dTimeString = String.valueOf(depTime);
flight[3] = dTimeString;

java.sql.Time arrTime = flightInfo.getArrTime();
String aTimeString = String.valueOf(arrTime);
flight[4] = aTimeString;

flight[5] = flightInfo.getAirline();
return flight;
}

Basically I am trying to achieve something like this - pass a string
to the method so that from a DB table, it displays all the records
from the columns in an array style.

Can anyone suggest me some improvement to the above.

Thank you.
 
V

VisionSet

Mohammed Mazid said:
I changed my code as I initially used an Object arrays, I decided to
use String array instead as I can convert from a date/time to a
string.

However, when I compiled and run, it displayed a nonsense output.
Something in the nature of "java.lang.String@28934".

Here is my latest code fragment. ....
out.println(displayFlight(flight1));

// Return an array of flight details
public String[] displayFlight(String flightNo) ....
Basically I am trying to achieve something like this - pass a string
to the method so that from a DB table, it displays all the records
from the columns in an array style.

An array does not implement toString(), you will therefore only get the
class & hashCode if you try and print it.

some options:

1/ iterate through the array printing out each in turn
2/ wrap your array in a class that implements toString() to deliver what you
want
 
J

Jose Rubio

See below...

Mohammed Mazid said:
I changed my code as I initially used an Object arrays, I decided to
use String array instead as I can convert from a date/time to a
string.

However, when I compiled and run, it displayed a nonsense output.
Something in the nature of "java.lang.String@28934".

This is not nonsense. That's how Java displays an object.
Here is my latest code fragment.

//This will be passed to the method displayFlights
String flight1 = "BA123";

String flightArray[] = new String[6];

for (int i = 0; i < flightArray.length; i++)
{
out.println("<td>" );
out.println(displayFlight(flight1));

You're displaying an array, not the actual flight for the first element of
the array. That's why you see the nonsense you called above.

Accoridng to the method below, change the loop above to:

String flightArray[] = displayFlight( flight1 );
for(int i=0; i < flightArray.length; i++ )
{
out.println( "<td">);
out.println( flightArray[ i ] );
out.println("</td>" );
out.println("<br>");
}
}

// Return an array of flight details
public String[] displayFlight(String flightNo)
{
String[] flight = new String[6];
SetupFlightDB flightDB = new SetupFlightDB();
FlightInformation flightInfo = flightDB.flightDetails(flightNo);

flight[0] = flightInfo.getFlightNo();

java.sql.Date flightDate = flightInfo.getFlightDate();
String dateString = String.valueOf(flightDate);
flight[1] = dateString;

flight[2] = flightInfo.getDestination();

java.sql.Time depTime = flightInfo.getDepTime();
String dTimeString = String.valueOf(depTime);
flight[3] = dTimeString;

java.sql.Time arrTime = flightInfo.getArrTime();
String aTimeString = String.valueOf(arrTime);
flight[4] = aTimeString;

flight[5] = flightInfo.getAirline();
return flight;
}

Basically I am trying to achieve something like this - pass a string
to the method so that from a DB table, it displays all the records
from the columns in an array style.

Can anyone suggest me some improvement to the above.

Thank you.

Hope it helps.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top