Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters back and forth

Status
Not open for further replies.

livingenzyme

Technical User
Nov 9, 2010
5
US
How do I pass multiple parameters back and forth between main and subroutine? Say I have the following program.


public class test {
public static void main(String[] args) {
char a=' ';
int b=1;
sub(a,b);
System.out.print(a +" " +b);
}
static void sub(char a,int b) {
a='h';
b=15;
return new Object(a,b);

}

}
Obviously, the ouput isn't going to be "h 15". What do I need to change for a and b to be passed back to main?

PS - I must say I am frustrated by java.
 
A Java method cannot return more than one parameter. You would need to encapsulate them in a new class or maybe use a Vector

Cheers,
Dian
 
In other words, java sucks. I should have known this when I started learning it.
 
Well, or maybe you suck? When you return something (last line):
Code:
    static void sub (char a,int b) {
        a='h';
        b=15;
        return new Object (a,b);   
    }
you have to declare it:

Code:
    static Object sub (char a,int b) {
        a='h';
        b=15;
        return new Object (a, b);   
    }
But here the compiler will have a reason to complain: There is no constructor for object which takes 2 parameters. You may use a self defined Object CI, and declare to return that:

Code:
    class CI {
        char c;
        int i;
        new CI (char c, int i) {
            this.c = c;
            this.i = i;
        }
    }

    static CI sub (char a,int b) {
        a = 'h';
        b = 15;
        return new CI (a, b);   
    }

To use that object, you have to assign the result of your methodcall to an object:
Code:
    public static void main (String[] args) {
        char a = ' ';
        int b = 1;
        CI ci = sub (a, b);
        System.out.print (ci.a + " " + ci.b);
    }


don't visit my homepage:
 
Yes, I do suck. I can't get your example to compile.


public class test {
public static void main (String[] args) {
char a = ' ';
int b = 1;
CI ci = sub (a, b);
System.out.print (ci.a + " " + ci.b);
}

class CI {
char c;
int i;
new CI (char c, int i) {
this.c = c;
this.i = i;
}
}

static CI sub (char a,int b) {
a = 'h';
b = 15;
return new CI (a, b);
}
}
 
Yes, you're right in 3 ways:
a) The CTOR can have a visibility modifier like 'public, private, protected' or nothing but not new (CI).
b) I didn't make visible, that the static method 'sub' needs a surrounding class.
c) I named ci.c and ci.i in the main-method ci.a and ci.b.
This is now a compilable file:
Code:
class CI {
	char c;
	int i;
	
	CI (char c, int i) 
	{
		this.c = c;
		this.i = i;
	}
}

public class CiTest
{
	static CI sub (char a, int b) 
	{
		a = 'h';
		b = 15;
		return new CI (a, b);   
	}

	public static void main (String[] args) {
		char a = ' ';
		int b = 1;
		CI ci = sub (a, b);
		System.out.print (ci.c + " " + ci.i);
	}
}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top