• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing an array in method  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried other forums, but don't have much luck and always end up coming back to CodeRanch. I need to pass user input from the main method, which is then validated using another method that is returned as a valid score, and then I pass the valid input to another method that stores the data in an array. The array is initialized within the method. I tried to use an if-else statement to initialize the array, because I originally did this at the beginning of the method. I soon learned that I was creating a new array everything I accessed the method. Needless to say, this isn't working either.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, this method doesn't compile because diverScores is declared inside the if statement:




I don't think that is what you want to do though. I think you want to pass a third parameter to inputAllScores() which would be the array itself. (validScores)
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is where my problem lies. The purpose of the assignment is to initialize the array within the method though. To pass the array as a parameter would negate any intiailization..no?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the if statement supposed to do? Right now it does do anything more than:

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose in doing it that way was to initialize the array only once. Originally I had no if else statement and the array was initialized at the beginning of the method. The problem with that was it was filling a new array with one value instead of building an array with each judges score.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Smiths wrote:Yes, that is where my problem lies. The purpose of the assignment is to initialize the array within the method though. To pass the array as a parameter would negate any intiailization..no?


You have to initialize the array in the method? What about declaring it outside the method but initializing it inside?
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. I was combining the declaration of the array as one in the same as the initialization. Thank you guys.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!