'Prompting user to enter five people's weights. Then store them in an Array and also output them

  1. Prompt the user to enter five numbers, being five people's weights. Store the numbers in an array of doubles. Output the array's numbers on one line, each number followed by one space.

I have tried the following code:

import java.util.Scanner;

 public class PeopleWeights {
 public static void main(String[] args) {
 Scanner reader = new Scanner(System.in); 


 System.out.println("Enter weight 1: " + reader.nextDouble());
 double weightOne = reader.nextDouble();

 System.out.println("Enter weight 2: " + reader.nextDouble());
 double weightTwo = reader.nextDouble();

 System.out.println("Enter weight 3: " + reader.nextDouble());
 double weightThree = reader.nextDouble();

 System.out.println("Enter weight 4: " + reader.nextDouble());
 double weightFour = reader.nextDouble();

 System.out.println("Enter weight 5: " + reader.nextDouble());
 double weightFive = reader.nextDouble(); /* Type your code here. */

return;
    }
}

I am sorry if my point of confusion is all over the place. But in short I'm having trouble with:

  1. Get user input to register in doubles.
  2. Get those inputs to be stored in an array.
  3. displaying those values in a manner like shown below.
  Enter weight 1:
  236.0
  Enter weight 2:
  89.5
  Enter weight 3:
  142.0
  Enter weight 4:
  166.3
  Enter weight 5:
  93.0
  You entered: 236.0 89.5 142.0 166.3 93.0


Solution 1:[1]

At first create an array:

// create array of doubles including 5 fields
double[] MyArray = new double[5];

And use for loop to iterate over it and store values in it:

for(int x = 0; x < MyArray.length; x = x + 1) 
{
         System.out.println("Enter value: ");
         MyArray[x] = reader.nextDouble();
}

Solution 2:[2]

I know this is a little late but I am going through this same activity in zyBooks labs. I noticed the above code is just a little off. Part of the activity is to have the output look like

Enter weight 1: 236
Enter weight 2: 89.5
Enter weight 3: 142
Enter weight 4: 166.3
Enter weight 5: 93

The above code will only out put as

Enter weight: 236
Enter weight: 89.5
Enter weight: 142
Enter weight: 166.3
Enter weight: 93

to fix that you need to do a little tweaking to your output simply by adding the index into your print statement as so

for (i = 0; i < MyArray.length; i = ++i) {
   System.out.println("Enter value " + (i + 1) + ": ");
   MyArray[i] = reader.nextDouble();
}

You need to add 1 to the index otherwise you'd start at 0 and end at 4. I have provided my code below for anyone thats needing some extra help with this assignment. It did pass all 6 checks 100% though it may not be the most efficient.

import java.util.Scanner;

public class PeopleWeights {
   public static void main(String[] args) {
      /* Type your code here. */
      Scanner scnr = new Scanner(System.in);
      final int NUM_ELEMENTS = 5;
      double[] userVals = new double[NUM_ELEMENTS];
      int i;
      int x;

      for (i = 0; i < NUM_ELEMENTS; ++i) {
         System.out.println("Enter weight " + (i + 1) + ": ");
         userVals[i] = scnr.nextDouble();         
      }  

      System.out.println("");
      System.out.print("You entered: ");
      for (i = 0; i < NUM_ELEMENTS; ++i) {         
         System.out.print(userVals[i] + " ");
      }

      System.out.println("");
      double totalWeight = 0.0;
      for (i = 0; i < NUM_ELEMENTS; ++i) {
         totalWeight += userVals[i];
      }
      System.out.println("Total weight: " + totalWeight);

      double averageWeight = 0.0;
      averageWeight = totalWeight / NUM_ELEMENTS;
      System.out.println("Average weight: " + averageWeight);

      double maxWeight = userVals[0];
      for (i =0; i < NUM_ELEMENTS; ++i) {
         if (userVals[i] > maxWeight) {
            maxWeight = userVals[i];
         }
      }
      System.out.println("Max weight: " + maxWeight);

      return;
   }
}

Solution 3:[3]

here is some code that passed

    import java.util.Scanner; //import the scanner for our user inputs
    public class PeopleWeights {
        public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in); 
        final int NUM_ELEMENTS = 5;            
        double [] userVals = new double[NUM_ELEMENTS]; 
        int i;                                         // Declaring variable i to use in for loop
        double sumVal;                                // Declaring sum variable to be used when calculating sum
        double maxVal;                                // Declaring maxVal variable to calculate max weight
  
        for (i = 0; i < userVals.length; ++i) {     // for loop that prompts and grabs user input to be used in array
            userVals[i] = scnr.nextDouble();
            System.out.println("Enter weight " + (i + 1) + ": ");
        }
     
        System.out.println();                       // Creating new lines to match output of the template in zybooks
        System.out.print("You entered: ");
  
        for (i = 0; i < userVals.length; ++i) {    // for loop that prints the users input
           System.out.print(userVals[i] + " ");
        }
                                            // for loop that calculates sum of users input "Total weight"
        sumVal = 0.0;
        for (i = 0; i < userVals.length; ++i) {
           sumVal = sumVal + userVals[i];
        }
  
        System.out.println();                    // Prints the total weight to match zybooks template
        System.out.print("Total weight: " + sumVal);
  
        System.out.println();                   // Calculates and Prints Average weight to match zybooks template
        System.out.print("Average weight: " + (sumVal / NUM_ELEMENTS));
  
        System.out.println();                  // Print statement for whitespace to match zybooks template
  
        maxVal = userVals[0];                 // Determine max weight, userVal[0] is the max so far
        for (i = 0; i < userVals.length; ++i) { // for loop and if statement to calculate max weight
           if (userVals[i] > maxVal) {
              maxVal = userVals[i];
           }
        }
  
        System.out.print("Max weight: " + maxVal);     // Prints max weight to match output of zybooks template
        System.out.println();                          // Print statement to match whitespace of zybooks template
  
  
        return;
     }
  }
   

Solution 4:[4]

   import java.util.Scanner;

public class PeopleWeights {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int peopleNum = 5; // amount of people being weighed 
double[] peopleWeight  = new double[peopleNum]; // the array 
int i;
double sumVal; // all elements added together 
double maxVal; // the maximum element
 // tell them to fill the array
 System.out.println("Enter weight 1:");
 System.out.println("Enter weight 2:");
 System.out.println("Enter weight 3:");
 System.out.println("Enter weight 4:");
 System.out.println("Enter weight 5:");
 // printing out what they typed
 System.out.print("You entered: ");
 for (i = 0; i < peopleWeight.length; ++i) {
 peopleWeight[i] = scnr.nextDouble();
 System.out.print( peopleWeight[i] + " ");
 }
 System.out.println(""); // adding all elements together
 sumVal = 0;
 for (i = 0; i < peopleWeight.length; ++i) {
 sumVal = sumVal + peopleWeight[i]  ;
 }
 maxVal = 0; // getting the max value
 for (i = 0; i < peopleWeight.length; ++i) {
 if (peopleWeight[i] > maxVal) {
 maxVal = peopleWeight[i];
 }
 } // the printing format
 System.out.println("");
 System.out.println("Total weight: " + sumVal);
 System.out.print("Average weight: ");
 System.out.println(sumVal / 5); 
 System.out.print("Max weight: ");
 System.out.println(maxVal);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JĂșlius Marko
Solution 2 RobertB922
Solution 3 user17047987
Solution 4 Nathan Humphrey