PDA

View Full Version : Method returning Multiple Values


adi501
07-15-2008, 12:27 AM
Hello .

I am trying to return multiple values from a method and i am unable to dothat.
Can anybody can help me in this regard.

The following is the Method i have.



public double getDistance(String City1 , String City2){

double lat1=0.0, lat2=0.0, long1=0.0, long2=0.0;
ResultSet rs = null;
Connection conn = null;
MakeConnection oMakeConnection = new MakeConnection();
conn = oMakeConnection.getConnection();
String sql = null;
sql = "SELECT LATITUDE, LONGITUDE from zipcodes WHERE city = ?";
java.sql.PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
pstmt.setString(1,City1);
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
rs=pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
if(rs.next())
{
long1 = rs.getDouble("longitude");
lat1 = rs.getDouble("latitude");
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
String sql1 = null;
sql1 = "SELECT LATITUDE,LONGITUDE from zipcodes WHERE city = ? ";
pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
pstmt.setString(1,City2);
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
rs=pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
if(rs.next())
{
long2 = rs.getDouble("longitude");
lat2 = rs.getDouble("latitude");
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return long1;
return long2;
return lat1;
return lat2;
}




I cant return long1, long2, lat1, lat2.

I want all the values. How can i do that ??

Waiting for the reply.
Thanks in Advance.

brad211987
07-15-2008, 04:28 AM
First, why do you want to do that in a method named getDistance? Map coordinates don't equate to distance. As for getting the values, you would be much better off to use get/set methods for each property here. For example you could declare instance variables for the class and access them as follows:


private double long1;

public void setLong1(double long1)
{
this.long1 = long1;
}

public double getLong1()
{
return long1;
}


EDIT: For the record, I'm not aware of a way to return multiple values in java, or of a situation in which it would be appropriate.

BubikolRamios
07-15-2008, 06:47 AM
nothing is passed by reference in java, everything by value, well not all...
you could try something like this



StrinfBuffer sb = new StringBuffer();
public double getDistance(String City1 , String City2, sb)
{
....
sb.append ....
// no need for return sb is by reference
// so it comes out filled as you did fill it
}

... writing this out of head, so ...

jerry62704
07-15-2008, 06:19 PM
You can return a collection, but you need to back up and ask why you want one function to do three things.