Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   passing variables from one method to another? (http://www.programmingforums.org/showthread.php?t=10015)

tumbleTetris May 27th, 2006 8:11 AM

passing variables from one method to another?
 
:

public class something
{
        public void methodOne()
        {
                int i = 0;
                int x = 4;
                String word = "hello";
               
                int[] abc = new int[26];
        while( i < 26)
        {
            abc[i] = x;
            x++;
            i++;
        }
       
        methodTwo(i, x, word, abc);
        }


        public void methodTwo(i, x, word, abc)
        {
                int a = i;
                int b = x;
                String c = word;
                int d = abc;
        }


}

I tried that, but it didn't work, I got an <identifier>expected and ')' expected error...

Toro May 27th, 2006 8:33 AM

methodTwo doesn't have any variable types declared.
[PHP]
//this works
public class something
{
public void methodOne()
{
int i = 0;
int x = 4;
String word = "hello";

int[] abc = new int[26];
while( i < 26)
{
abc[i] = x;
x++;
i++;
}

methodTwo(i, x, word, abc);
}
public void methodTwo( int i, int x, String word, int abc[])
{
int a = i;
int b = x;
String c = word;
int d[] = abc;
}
}
[/PHP]
btw, please work at the issue before posting, these are common program errors and it shows us that you do not know what you are doing. I advise you to read up so references.

tumbleTetris May 27th, 2006 8:42 AM

yeah, thanks, I noticed.

I have been a little jumpy recently...

tumbleTetris May 28th, 2006 8:24 AM

well I just have hit another stumbling block, I can't invoke a method that has a passed array within its argument.

like if I have:
:

    public void initAbc(){
        String[] abc = new String[7];
        //destinations within array
        abc[0] = "[1]  fasd";
        abc[1] = "[2]  fdas";
        abc[2] = "[3]  dfafsd";
        abc[3] = "[4]  Hedfrmes";
        abc[4] = "[5]  dfas";
        abc[5] = "[6]  dfas";
        abc[6] = "[7]  asdfasfds";
       
        printDestinations( abc );
    }
   
    public void printAbc( String abc[] )
    {
        int i = 0;
        while ( i < 7 )
        {
            System.out.println(abc[i]);
            i++;
        }
    }


if I try to invoke it in another class, for instance:

:


classSomething abc = new classSomething();

abc.initAbc();

abc.printAbc(); <-- doesn't work.


Toro May 28th, 2006 10:22 AM

First of all, it doesn't work because printAbc() contains arguments and to me it looks like it doesnt have any arguments! And doesn't initAbc() call printtAbc automatically?


All times are GMT -5. The time now is 7:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC