Results 1 to 9 of 9
- 10-28-2012, 03:00 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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(); } }
- 10-28-2012, 03:11 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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?
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.)Java Code:public void callReturnValues() { int[] values = return1(); // print out values }
Or perhaps you want the magic to happen in main()?
Notice again a variable (test) is used and assigned to using what the "new" operator returns.Java Code:public static void main(String[] args) { Test test = new Test(); int[] values = test.return1(); // do something with values }
Or perhaps, something else...?
- 10-28-2012, 03:26 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: How do I use a variable that has been returned
I think I got I needed with that now I have this.
ThanksJava 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(); } }Last edited by jwl; 10-28-2012 at 03:05 AM.
- 10-28-2012, 03:25 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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.
This is what happens when I run it: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 } }
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.
- 10-28-2012, 04:02 AM #5
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: How do I use a variable that has been returned
Doing all this in your constructor may not be what you ultimately want.... look at what pbrockway2 suggested, much cleaner.Java Code:public void testMethod() { int[] data = readInputFile(); //You've already closed the file here
- 10-28-2012, 04:06 AM #6
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: How do I use a variable that has been returned
Doing all this in your constructor may not be what you ultimately want.... look at what pbrockway2 suggested, much cleaner.Java Code:public void testMethod() { int[] data = readInputFile(); //You've already closed the file here
...
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.
- 10-28-2012, 04:19 AM #7
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
- 10-28-2012, 04:23 AM #8
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: How do I use a variable that has been returned
- 10-28-2012, 04:47 AM #9
Similar Threads
-
Filtering returned results
By eohalloran in forum New To JavaReplies: 5Last Post: 01-25-2012, 05:14 PM -
No value returned from outside method? Confused, please help
By Baconator in forum New To JavaReplies: 1Last Post: 10-21-2011, 10:51 AM -
Null-Object returned though returned object is working
By Boreeas in forum New To JavaReplies: 5Last Post: 09-17-2011, 01:35 AM -
value returned is zero when it is not
By gedas in forum New To JavaReplies: 21Last Post: 03-27-2011, 07:23 PM -
get url returned by search
By avizana in forum NetworkingReplies: 2Last Post: 03-14-2011, 07:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks