INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Passing parameters back and forth

Passing parameters back and forth

(OP)
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.

RE: Passing parameters back and forth

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

RE: Passing parameters back and forth

(OP)
In other words, java sucks.  I should have known this when I started learning it.   

RE: Passing parameters back and forth

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: http://home.arcor.de/hirnstrom/minis/index.html

RE: Passing parameters back and forth

(OP)
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);
    }
}
 

RE: Passing parameters back and forth

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: http://home.arcor.de/hirnstrom/minis/index.html

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close