0

I've gotten stuck on a question for a class and researching hasn't helped. So far I have the original array that creates 10 random numbers between 100 and 200, an Arrays class that sorts it from least to greatest, and a 2nd method that uses the original array as its argument, returns the result, and is printed in a for each loop under the main method. I need to add a statement to the for each loop that takes each returned value and adds them together, then prints them on the 3rd output line. I keep trying, but am getting the total added to the end of each value.

import java.util.Arrays;

public class Project7and81 {
public static void main(String[] args) {
    int[] randnums = new int[10];
    for (int i =0; i < randnums.length; i++){
        randnums[i] = 100 + (int)(Math.random() * (200-100) + 1);}
        java.util.Arrays.sort(randnums);
        System.out.println(Arrays.toString(randnums));  
    for (double e: doubleMyArray(randnums)){
        System.out.print((int) e + " ");

    }
    }
public static int[] doubleMyArray(int[] randnums){
    int[] doubledNums = new int[randnums.length];
    for (int i = 0; i < randnums.length; ++i)
        doubledNums[i] = randnums[i] * 2;
        return doubledNums;
}
}   

1 Answer 1

0

You're updating a value whose scope is limited; it exists only within your loop.

 public static int[] doubleMyArray(int[] randnums) {
   for (int e: randnums)
   {
        e = (e * 2);
        // Scope of e ends here; e stops existing.
   }
   return randnums;
 }

You should instead update either (depending on your preference) the values in the original array, or into a new array. Since you want to update the passed-in array in place (which is poor practice, don't do this unless explicitly directed to!), the loop is:

 public static int[] doubleMyArray(int[] randnums) {
   int i = 0;
   for (int value : randnums) // Could do this as for (;;) loop, which would be nicer imo
   {
        randnums[i++] = value * 2;
   }
   return randnums;
 }

To display the doubled numbers and return the sum (but not display the sum), use this loop:

 public static int displayArrayAndReturnSum(int[] randnums) {
     int total = 0;
     for (int value : randnums)
     {
         System.out.print(" " + value);
         total += value;
     }
     System.out.println("");
     return total;
 }
7
  • I may not be understanding this correctly, but I don't see a for each statement in the answers? I see two for loops, but for the assignment I have to use a for each to modify the contents of the original array. Am I not reading it correctly?
    – Xiokraze
    Nov 5, 2015 at 13:14
  • I think that's where I'm getting lost. I have the original array set as the argument for the method, but am not sure how to call the values, modify them, and then return them for the print statement.
    – Xiokraze
    Nov 5, 2015 at 13:20
  • Your answer works for sure, but I'm not understanding how to use a for each loop. That's where I'm having trouble.
    – Xiokraze
    Nov 5, 2015 at 13:26
  • Paul, I really appreciate the comments. I actually misread the instructions, which was throwing me off haha. Your initial answer worked just fine. The for each loop was to be placed in the main function and call each doubled value. I got that, but now I need to also add all of the returned values from the method together and print that. I can't seem to figure out how to pull all the values, add them together, and print the sum by itself. I'll update the original post with my new code.
    – Xiokraze
    Nov 5, 2015 at 20:44
  • Here's the whole question. Use a for loop to generate an array of ten random integers, all in the range from 100 to 200, inclusive. Use the Arrays class to both sort and display the entire array. Next, pass the array as the sole argument to a method that doubles each element of the array and then returns the array. Use a foreach loop to show all elements in the returned array on one line separated by a single space. This latter loop should also determine the sum of all elements in the returned array. Display the sum with a "thousands" comma as in the sample output.
    – Xiokraze
    Nov 6, 2015 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.