Results 1 to 19 of 19
  1. #1
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default Can i Pass a variable as a parameter from one method to other method

    Hi All,

    I am having two different methos
    1. WriteFile(String f,String arrr)
    2. ReadFile(file f1, String s1);


    I want to pass a variable from ReadFile(file f1, String s1) to the WriteFile(String f,String arrr).

    How can i do this please provide me some guidelines.

    Thanks In Advance.

  2. #2
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default like this?

    here's an example of a method calling another method and passing it a variable:

    Java Code:
        public void methodOne(){
              String varOne = "some string 1";
              String varTwo="some string 2"
    
             methodTwo(varOne, varTwo);
        }
    
    
        public void methodTwo(String one , String two){
            this.jLabel1.setText(one);
            this.jLabel2.setText(two);
    
        }
    hope this helps

  3. #3
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks jasonwucinski,

    Can i use varOne Outside the method to pass to methodTwo(), without calling it in methodOne();

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Not if you define it locally(in the method) if you make it a class variable it would work how you want though.

  5. #5
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    How can i Use that Local variable outside to the method..

  6. #6
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    sure, if its declared as a global variable in your class. like this:

    Java Code:
    public class MyClass {
    
       private String varOne = "a";
    
    public void myMethodOne(){
    //this method as access to varOne
    String NewVar = varOne + "b";
    myMethodTwo(NewVar);
    
    }
    public void myMethodTwo(String getVar){
    //this method as access to varOne which would = "a"
    // the passed variable, getVar would = "ab"
    
    }
    
    
     
    }
    is this what you mean? in this example, there is a global variable called VarOne, which all methods in the class can read. Method one takes that variable, appends it with a new string, then passes it to methodTwo. is this what you meant?

  7. #7
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    Also, if you need to pass variable between classes, you will need to use an interface, but that's a different, more complicated, topic

  8. #8
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks jasonwucinski, sunde887,

    Java Code:
    public static void WriteFile(String f, String arr)
    {
    	try{
    
    	File f1=new File(f);
    	f1.createNewFile();
    	FileWriter fstream = new FileWriter(f,true);
    	BufferedWriter out = new BufferedWriter(fstream);
    	out.write(arr);
    	out.write(System.getProperty("line.separator"));
    	System.out.println("Hello Done");
    
    	out.close();
    
    }catch(Exception e){
    e.printStackTrace();
    }
    This is my one method
    Java Code:
    public static void ReadFile(File f1, String f2)
    {
    	Writer output = null;
    	//int i=2;
           Sheet sheet;
             try {
    				output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f2),"UTF8"));
    		                sheet = SpreadSheet.createFromFile(f1).getSheet(0);
    				int nColCount = sheet.getColumnCount();
    				int nRowCount = sheet.getRowCount();
    
    				System.out.println("Rows :"+nRowCount);
    				System.out.println("Cols :"+nColCount);
    				MutableCell cell = null;
    
                for(int nRowIndex = 0; nRowIndex < nRowCount; nRowIndex++)
                {
                    int nColIndex = 0;
                    for( ;nColIndex < nColCount; nColIndex++)
                   {
                        cell=sheet.getCellAt(nColIndex, nRowIndex);
    				   System.out.print(cell.getValue()+ "");
    				   output.write(cell.getValue()+"");
    				   output.write(System.getProperty("line.separator"));
    				//}
                    System.out.println();
    
               }
                 }
    	    output.close();
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    		}
    And this is Another Method, Now I want to use value of this cell.getValue() as parameter to the WriteFile method. How can i achieve this.

    Thanks

  9. #9
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    i find it strange you were able to write that code but not be able to understand variable scope. anyway, look how your writeFile method is constructed. it accepts two variables, both strings. one, presumably is the name of a file, im not sure what the other is for. so, you would call WriteFile like this:

    Java Code:
    WriteFile("MyFilePath://MyFileName", "some string")
    so, you could either add another variable to the writeFile like this:

    Java Code:
    public static void WriteFile(String f, String arr, String AnotherVar)
    which means you would call it like this:


    Java Code:
    WriteFile("MyFilePath://MyFileName", "some string", "one more string")
    or, you create a global variable and use that. try reading this, as you wont get far in any programming course without understanding variable and their scope:

    Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by Anagha View Post
    How can i achieve this.
    Are you serious? You are basically getting people on the forum to write your code line by line. How about you think for yourself.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'd like to nitpick and say that it isn't technically a global variable, instead it's an instance variable. It's honestly not a huge difference but global variables in java are more like a static variable.

    When you want a variable that can be used in multiple methods you want to give the class an instance variable.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Nitpick the nitpick. Java does not have global variables at all. ;)

  13. #13
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hi Juncky,

    I am not asking to write a code line by line... I am requesting for any one who can suggest me the best way to achieve my goal not the Code.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    Nitpick the nitpick. Java does not have global variables at all. ;)
    lol. I like this post.

    @op: If you intend to receive advice, and not answers, it's best to show what you tried as well as asking the questions. A lot of people come here "asking" for help, when they really are just looking for the direct answer.

  15. #15
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    sorry, most of my programming experience is in the .Net's. global variables or class fields, what's the difference lol.

  16. #16
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hi sunde887,

    So I need to use that variables as a class variable then only i can use into the method.

    @ jasonwucinski,
    Java Code:
    WriteFile("MyFilePath://MyFileName", "some string")
    The Second parameter i want to use is that from ReadFile(cell.getValue()+"");

    Thanks...

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    @Jason, perhaps it's a small difference, however there is absolutely a difference between a class variable and a global one, a global variable is something that can be used by anything directly. A static variable is close to this but still a bit different.

    A class variable appears like this inside of the class. However outside of the class it cannot be accessed directly and requires you to first create an instance of the object to use it "globally"

    I do, of course understand what you were trying to convey, I just felt like I needed to point out the difference.
    Last edited by sunde887; 04-15-2011 at 08:16 AM.

  18. #18
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hi All,

    I am Still Confuse that how do i use local variable of one method as parameter for another method.

    Please Help me...

    Tell me What should i use for this..


    Thanks in Advance..

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    I'm sure you have already been told. You call the method and pass the value as a parameter.
    Java Code:
    class Foo {
        Bar b = new Bar();
    
        public void someMethod() {
            int value = 10;
            methodInSameClass(value);
            b.methodInOtherClass(value);
        }
    
        private void methodInSameClass(int v) {
            System.out.println(2 * v);
        }
    }
    
    class Bar {
        public methodInOtherClass(int v) {
            system.out.println(4 * v);
        }
    }

Similar Threads

  1. Class<T> in method parameter
    By Onra in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 12:12 AM
  2. Creating array in method parameter
    By Dipke in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 09:18 AM
  3. pass value inside method A to method B
    By masokis in forum New To Java
    Replies: 5
    Last Post: 09-01-2010, 04:06 AM
  4. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM
  5. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 06:24 PM

Posting Permissions

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