Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2008
    Location
    Falls Chuch, VA
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Method returning Multiple Values

    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.

    Code:
     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.

  2. Users who have thanked adi501 for this post:

    troublemaker (07-15-2008)

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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:

    PHP Code:
    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.

  • Users who have thanked brad211987 for this post:

    troublemaker (07-15-2008)

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    nothing is passed by reference in java, everything by value, well not all...
    you could try something like this


    Code:
    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 ...

  • #4
    Senior Coder jerry62704's Avatar
    Join Date
    Oct 2007
    Location
    Springfield, IL
    Posts
    1,100
    Thanks
    13
    Thanked 87 Times in 87 Posts
    You can return a collection, but you need to back up and ask why you want one function to do three things.
    .
    .
    ...and gladly would he learn and gladly teach

    Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •