Multi Variable return

Tring to return a String and int value

Page 1 of 1

12 Replies - 7009 Views - Last Post: 30 January 2009 - 09:11 PM Rate Topic: -----

#1 simonc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 17-November 08

Multi Variable return

Post icon  Posted 29 January 2009 - 07:22 AM

Hi, Is it possible to return more than one variable from a method (say a String vairable and an int variable)?

If so how would you do it..

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Multi Variable return

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Multi Variable return

Posted 29 January 2009 - 07:42 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this: :code:

Thank you for helping us helping you
Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Multi Variable return

Posted 29 January 2009 - 08:37 AM

No you can only return one type from a method, which is specified by the return type. Though ways to get around this is creating objects which store the different types as fields and then returning the object.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Multi Variable return

Posted 29 January 2009 - 05:29 PM

View Postsimonc, on 29 Jan, 2009 - 06:22 AM, said:

Hi, Is it possible to return more than one variable from a method (say a String vairable and an int variable)?

If so how would you do it..

Thanks


No you can't but it is easy to write a class that would contain those fields

class StrAndInt {
   String str;
   int num;

   StrAndInt(String str, int num) {
	   this.str = str;
	   this.num = num;
   }
}

class MyApplication {
   ....
   ....
   StrAndInt myMethod() {
	  ....
	  ....
	  return new StrAndInt("The value returned", 10);
   }


Was This Post Helpful? 1
  • +
  • -

#5 simonc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 17-November 08

Re: Multi Variable return

Posted 30 January 2009 - 02:28 AM

Hi, new to java and trying to write a simple test program which has two variables (a String str1 and an integer int1) which I set to "Hi" and 10 in main send them to the class VariableSet where I try to manipulate the variables (the String to "Hi there" and the integer to 20). Then send the result to StrAndInt for output. This is just a bear bones test program which I will adapt once I have this problem sorted. Could some one let m know what I've done wrong please.
Many thanks

class MyApplication {
	
	public static void main(String args[]) {
	
		VariableSet test = new VariableSet("Hi ",10);
	}

}




public class VariableSet {
	
	String str1;
	int num1;
	
	StrAndInt myMethod(str1, num1) {
		  
		str1 = str1 + "there";
		num1 = num1 + 10;

			  return new StrAndInt(str1, num1);
		   }
		   
}



public class StrAndInt {
	   String str1;
	   int num1;

	   StrAndInt(String str1, int num1) {
		   this.str1 = str1;
		   this.num1 = num1;
		   
		   System.out.println(str1);
		   System.out.println(num1);
	   }
	}



Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 11,469
  • Joined: 20-September 08

Re: Multi Variable return

Posted 30 January 2009 - 02:44 AM

You need the correct ctor in the class you're instantiating:

public class VariableSet {
        private int intVal;
        private String strVal;

        public VariableSet(int intVal, String strVal) {
            this.intVal = intVal;
            this.strVal = strVal;
        }

        public int getIntVal() {
                return this.intVal;
        }

        public void setIntVal(int intVal) {
                this.intVal = intVal;
        }

        public String getStrVal() {
                return this.strVal;
        }

        public void setStrVal(String strVal) {
                this.strVal = strVal;
        }
}



Was This Post Helpful? 0
  • +
  • -

#7 simonc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 17-November 08

Re: Multi Variable return

Posted 30 January 2009 - 05:04 AM

Thanks, but stil having a problem getting the String and integer to output. Could someone have a look please. Have anotated where the errors are:

Thanks


class MyApplication {
	
	public static void main(String args[]) {
	
		VariableSet test = new VariableSet("Hi ", 10 ); //The local variable test is never read

	}

}




public class VariableSet {
	
	String str1;
	int num1;
	
	StrAndInt VariableSet(String str1, int num1){  //This method has a constructor name
		
		str1 = str1 + "there";
		num1 = num1 + 10;

			  return new StrAndInt(str1, num1);
		   }
		   
}





public class StrAndInt {
	   String str1;
	   int num1;

	   StrAndInt(String str1, int num1) {
		   this.str1 = str1;
		   this.num1 = num1;
		   
		   System.out.println(str1);
		   System.out.println(num1);
	   }
	}



Was This Post Helpful? 0
  • +
  • -

#8 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: Multi Variable return

Posted 30 January 2009 - 05:39 AM

Seems like the method you created is not the constructor because you have a return type.

Do something like this:

public class Test
{
	public void main(String[] args)
	{
		StringAndInt stringAndint = getStringAndInt();
		int returnedInt = stringAndint.getInteger();
		String returnedString = stringAndint.getString();
	}

	private StringAndInt getStringAndInt()
	{
		return new StringAndInt(10, "string");
	}


	private class StringAndInt
	{
		int integer;
		String string;

		private StringAndInt(int integer, String string)
		{
			this.integer = integer;
			this.string = string;
		}

		public int getInteger()
		{
			return integer;
		}

		public String getString()
		{
			return string;
		}
	}
}



Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

#9 johnmalloy  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 24-September 08

Re: Multi Variable return

Posted 30 January 2009 - 02:08 PM

Also do not forget that you can use the System.out.println("1 "+var1+"2 "+var2);
Was This Post Helpful? 0
  • +
  • -

#10 c0mrade  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Re: Multi Variable return

Posted 30 January 2009 - 06:41 PM

Other options... just so you know you have them

// Return an object array (quite ugly)
return new Object[] {new Integer(5), new String("hi")};

// Return a Java Collections object (also quite ugly)
List ret = new List();
ret.put(5);
ret.put("hi");
return ret;


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Multi Variable return

Posted 30 January 2009 - 08:24 PM

View Postg00se, on 30 Jan, 2009 - 01:44 AM, said:

You need the correct ctor in the class you're instantiating:

public class VariableSet {
        private int intVal;
        private String strVal;

        public VariableSet(int intVal, String strVal) {
            this.intVal = intVal;
            this.strVal = strVal;
        }

        public int getIntVal() {
                return this.intVal;
        }

        public void setIntVal(int intVal) {
                this.intVal = intVal;
        }

        public String getStrVal() {
                return this.strVal;
        }

        public void setStrVal(String strVal) {
                this.strVal = strVal;
        }
}




OK there are purists and purists
There is OO and OO
I personnaly think that an inner class (just used inside a class) does not have to declare its instances variables "private" and thus, no need for accessors
Was This Post Helpful? 0
  • +
  • -

#12 c0mrade  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Re: Multi Variable return

Posted 30 January 2009 - 09:05 PM

This thread is a repost - see "Return Statement"
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Multi Variable return

Posted 30 January 2009 - 09:11 PM

View Postc0mrade, on 30 Jan, 2009 - 08:05 PM, said:

This thread is a repost - see "Return Statement"

Topics merged
Next time do not hesitate to mail me directly
Thanks for letting us know about it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1