• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass array data between methods  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I pass the data within an initialized array from inside one method to another method of the same class? Will I need to return the array, assigning it to a temp array, which will then be passed as an argument to the other array? The idea is to create an array for an entire year, and be able to manipulate or edit data for a particular month using the other method.
Thanks for you any help.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote:How do I pass the data within an initialized array from inside one method to another method of the same class? Will I need to return the array, assigning it to a temp array, which will then be passed as an argument to the other array?


No, you can use a method that returns an array itself to pass the the data into another method.

Say foo() returns an array and you want to pass that array to bar(). foo( bar() ) is all you need.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:No, you can use a method that returns an array itself to pass the the data into another method.
Say foo() returns an array and you want to pass that array to bar(). foo( bar() ) is all you need.


Thank you and I think I understand that basic idea. I still don't know how I could use: inputTempForMonth(inputTempForYear()), and still have the parameters int month, int [][] temp. The array temp, will contain the high and low temps for the month to be changed.

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless I'm misunderstanding you, just add temperatureArray to the parameters in inputTempForMonth() and return the modified array.

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, we have not dealt with passing methods as a parameter, so we're not permitted to use that syntax. Now I have run into a separate issue, running the program doesn't allow me to enter a response to whether I want to modify a month, yes or no. And just executes the rest of the program -- inaccurately I might add. The output does not return the correct values.
What I have so far:

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is why is the program not allowing me to modify the temperature of one of the months? It will not allow me to enter a response before continuing to execute the program.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't want to use Scanner.nextLine(). This poorly named method scans until it finds a newline, and returns if it doesn't find any. You probably want Scanner.next().
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!