Results 1 to 9 of 9
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default How do I use a variable that has been returned

    Java Code:
    package test;
    
    public class Test {
        
        public Test(){
            callReturnValues();
        }
        
        public int[] return1() {
            int [] values = {30, 49, 43, 30, 60};
            return values;
        }
        
        public void callReturnValues() {
            // call values
            // print out values
        }
        public static void main(String[] args) {
            new Test();
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How do I use a variable that has been returned

    The only method that returns anything there is return1(). I'm not quite sure where you want to use what it returns. In callReturnValues() perhaps?

    Java Code:
    public void callReturnValues() {
        int[] values = return1();
        // print out values
    }
    As shown the first order of business is to assign the returned value to some variable. Then you can do whatever you like with it. (And whatever you do like to do with it should be reflected in the method's name: callReturnValues() is very nondescriptive.)

    Or perhaps you want the magic to happen in main()?

    Java Code:
    public static void main(String[] args) {
        Test test = new Test();
        int[] values = test.return1();
        // do something with values
    }
    Notice again a variable (test) is used and assigned to using what the "new" operator returns.

    Or perhaps, something else...?

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: How do I use a variable that has been returned

    I think I got I needed with that now I have this.

    Java Code:
    package test;
    
    public class Test {
        
        public Test(){
            callReturnValues();
        }
        
        public int[] return1() {
            int[] returnValues = {30, 49, 43, 30, 60};
            return returnValues; 
        }
        
        public void callReturnValues() {
            int[] returnValues = return1();
            System.out.println(returnValues[3]);
            // more stuff with returnValues[]
        }
        public static void main(String[] args) {
            new Test();
        }
    }
    Thanks
    Last edited by jwl; 10-28-2012 at 03:05 AM.

  4. #4
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: How do I use a variable that has been returned

    OK, That worked for that simpler program. Now I am running into a little more technical difficulties.

    Java Code:
    package test;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test {
        private Scanner read;
       private int numData;
        
       public Test() { // constructor which runs the methods
           openFile();
           readInputFile();
           closeFile();
           testMethod();
       }
        // Open File
       private void openFile() {
           try {
               read = new Scanner(new File("file.txt")); // reading integers from a file
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
       
       //Close File
       private void closeFile() {
           read.close();
       }
      
       //Read File and Store it in an array of size numData
       private int[] readInputFile() {
           numData = read.nextInt(); // Creating size of array from file
           read.nextLine();
           int[] data = new int[numData]; //Storing integers in an array
            for (int i = 0; i < numData; i++) {
                data[i] = read.nextInt();
                //System.out.printf("%s\n", data[i] + " array item: " +  i); //testing all data is read from file
           }
           return data;
       }
       
       public void testMethod() {
           int[] data = readInputFile();
           System.out.println(data[9]); // Testing that data from readInputFile() has transfered correctly 
       }
        
        public static void main(String[] args) {
            new Test(); // call constructor
        }
    }
    This is what happens when I run it:

    Exception in thread "main" java.lang.IllegalStateException: Scanner closed
    followed by more at ..... errors

    When you take the testMethod out and uncomment the test string in the readInputFile() as well as delete the test method call in the constructor the program runs fine.
    Last edited by jwl; 10-28-2012 at 03:52 AM.

  5. #5
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: How do I use a variable that has been returned

    Java Code:
      public void testMethod() {
           int[] data = readInputFile();  //You've already closed the file here
    Doing all this in your constructor may not be what you ultimately want.... look at what pbrockway2 suggested, much cleaner.

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: How do I use a variable that has been returned

    Java Code:
      public void testMethod() {
           int[] data = readInputFile();  //You've already closed the file here
    Doing all this in your constructor may not be what you ultimately want.... look at what pbrockway2 suggested, much cleaner.

    ...
    run closeFile() after testMethod() ;)

    Don't run readInputFile() in constructor ... it's called by testMethod(), Scanner will already be at the end of file by the time testMethod() tries to get results.

  7. #7
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: How do I use a variable that has been returned

    Quote Originally Posted by SJF View Post

    Doing all this in your constructor may not be what you ultimately want.... look at what pbrockway2 suggested, much cleaner.

    ...
    run closeFile() after testMethod() ;)
    All of that for a cut and paste error. Thank you guys.

  8. #8
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: How do I use a variable that has been returned


  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How do I use a variable that has been returned

    Terminology nitpick: methods can return values, not variables. The value returned by a method may be assigned to a variable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Filtering returned results
    By eohalloran in forum New To Java
    Replies: 5
    Last Post: 01-25-2012, 05:14 PM
  2. Replies: 1
    Last Post: 10-21-2011, 10:51 AM
  3. Replies: 5
    Last Post: 09-17-2011, 01:35 AM
  4. value returned is zero when it is not
    By gedas in forum New To Java
    Replies: 21
    Last Post: 03-27-2011, 07:23 PM
  5. get url returned by search
    By avizana in forum Networking
    Replies: 2
    Last Post: 03-14-2011, 07:43 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
  •