Results 1 to 10 of 10
  1. #1
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Method - Array Initialization

    I am passing input from the user to a method that will initialize an array of the data (scores in this case). The method is filling the entire array with the last input value.

    array initializer method
    Java Code:
    	public static float[] inputAllScores(float validScore) {
    		float[] diverScores = new float[7];
    
    		for (int i = 0; i < diverScores.length; i++) {
    			diverScores[i] = validScore;
    		}
    		return diverScores;
    	}
    //main method
    Java Code:
    for (int i = 0; i < judges; i++) {
    			float score = -1;
    			
    			while(score < 0 || score > 10) {
    			System.out.print("Judge " + (i + 1) + " Score: ");
    			score = input.nextFloat();
    			score = inputValidScore(score);
    			}
    			validScores = inputAllScores(score);			
    		}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Method - Array Initialization

    Look what that first method does: (in words):

    - create a new array;
    - fill each element wit the value 'validScore';
    - return the array.

    Your second method fiddles a bit and calls the first method in a loop, and reassigns 'validScores' with the array value returned by the first method. Guess the value of that variable when the loop has finished... (hint: it's the last returned array, filled with the value of the last 'score'...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Method - Array Initialization

    JosAH, thanks. that seems so obvious now that you mention it. If I change the inputAllScores method, I don't see how I can initialize the array within the method and pass the user input into the array without doing it this way. Unless I pass another int value that will act as the index value for the array within inputAllScores.

    intputAllScores(float score, int index) {
    float [] array = new float [7];

    array[index] = score;

    return array;
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Method - Array Initialization

    That doesn't work either, because your method creates a new array over and over again. In words: the method should set a value at a certain position in an array; this implies that the method should have three parameters, a value, an index position and an array; there is no need to return the array again, because the caller of the method already knows about the array; nor should that method create a new array over and over again, i.e. all it should do is set the value at a certain index position in the given array.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Method - Array Initialization

    Since you are initializing your array in a static method, you could have a static index with a static array that is bumped each time the inputAllScores is called. But to reuse the method you need to reset the index (which will wipe out previous scores). But a better way, imho, is to get away from arrays and use a List implementation (e.g. ArrayList). Then you can just keep appending scores to the end as necessary.
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Method - Array Initialization

    I see, then I must be interpreting the assignment requirements incorrectly:
    "A method name inputValidScore that inputs one valid score for one judge for one diver. This method will return the valid score.
    A method named inputAllScores that creates an array to store the scores for all judges for the diver. This method will fill the array with a valid score from each judge. This method does not take input arguments, but it does return the array of scores."

    What you are saying makes sense and I see the fault in the programming. When the assignment says create an array in inputAllScores, we actually create the array in the main method and then just add the values to that array within the inputAll method?

    Thank you

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Method - Array Initialization

    Now you're talking about two methods: one that inputs one valid score and one that conducts the entire process, by creating an array and sticking all scores in the array (by calling the first method in a loop) and returning the created array; that makes more sense ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    the_dude is offline Member
    Join Date
    Jan 2015
    Posts
    21
    Rep Power
    0

    Default Re: Method - Array Initialization

    Hey guys, I tried this, but my array variable is trapped in the if statement...how can I make it accessible to the else statement. I thought I had it figured out on the whiteboard.

    Java Code:
    public static float[] inputAllScores(float validScore, int index) {
    		
    		if(index == 0) {
    			float[] diverScores = new float[7];
    			diverScores[0] = validScore;
    		} else {
    			diverScores[index] = validScore;
    		}
    		return diverScores;
    	}

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Method - Array Initialization

    Declare it first thing in the method. But don't check if index = 0, check if the diverScores == null. Or pass the diverScores to the method itself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Method - Array Initialization

    Quote Originally Posted by the_dude View Post
    Hey guys, I tried this, but my array variable is trapped in the if statement...how can I make it accessible to the else statement. I thought I had it figured out on the whiteboard.

    Java Code:
    public static float[] inputAllScores(float validScore, int index) {
    		
    		if(index == 0) {
    			float[] diverScores = new float[7];
    			diverScores[0] = validScore;
    		} else {
    			diverScores[index] = validScore;
    		}
    		return diverScores;
    	}
    If I reread your reply #6 again, your method shouldn't take any parameters; have a look:

    Java Code:
    public static float[] inputAllScored() {
       float[] diverScores= new float[7];
       // ...
       // fill the above array using a method 'inputValidScore( ... )'
       // ...
       return diverScores;
    }
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Array of object initialization
    By rybo85 in forum New To Java
    Replies: 8
    Last Post: 05-03-2012, 06:49 AM
  2. Int Array Initialization Problem
    By dragstang86 in forum New To Java
    Replies: 5
    Last Post: 03-28-2012, 10:54 PM
  3. An Array of Objects (Class Initialization)
    By Chris_C in forum New To Java
    Replies: 9
    Last Post: 02-09-2011, 06:49 AM
  4. Array initialization difference
    By sln69 in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2008, 03:38 AM
  5. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 03:12 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
  •