-1

NOT ABLE TO CORRECT THIS CODE..

import java.text.DecimalFormat;

public class PeopleWeights {
    public static void main(String[] args) {
        DecimalFormat decFor = new DecimalFormat("0.00");
        int i = 0;
        int n = 5;
        double arr[]=new double[n];

        for(i = 0; i < n; i++){
            System.out.println("Enter weight " + (i+1)+": ");
        }
        System.out.print("\nYou entered: ");
        for(i = 0; i < n; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        double total = 0;
        double max = 0;
        for(i=0; i<n; i++){
            if(max < arr[i]){
                max = arr[i];
            }
            total = total + arr[i];
        }

        DecimalFormat df = new DecimalFormat("#.##");
        double average = total/n;
        System.out.println("Total weight: "+ df.format(total));
        System.out.println("Average weight: "+ df.format(average));
        System.out.println("Max weight: "+ df.format(max));

        return;
    }
}

THIS ARE THE ERRORS I KEEP GETTING.

  1. Compare output 0/1 Input 236 89.5 142 166.3 93 Your output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 0.0 0.0 0.0 0.0 0.0 Total weight: 0 Average Expected output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 236.0 89.5 142.0 166.3 93.0

  1. Compare output 0/1 Input 123.4 56 98 174 215.8 Your output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 0.0 0.0 0.0 0.0 0.0 Total weight: 0 Average Expected output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 123.4 56.0 98.0 174.0 215.8

  1. Compare output 0/1 Input 236 89.5 142 166.3 93 Your output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 0.0 0.0 0.0 0.0 0.0 Total weight: 0 Average weight: 0 Max weight: 0

Expected output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8

  1. Compare output 0/1 Input 236 89.5 142 166.3 93 Your output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 0.0 0.0 0.0 0.0 0.0 Total weight: 0 Average weight: 0 Max weight: 0

Expected output starts with Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8 Average weight: 145.35999999999999

  1. Compare output 0/1 Input 236 89.5 142 166.3 93 Your output Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 0.0 0.0 0.0 0.0 0.0 Total weight: 0 Average weight: 0 Max weight: 0

Expected output Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8 Average weight: 145.35999999999999 Max weight: 236.0

  1. Compare output 0/1 Input 123.4 56 98 174 215.8 Your output Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 0.0 0.0 0.0 0.0 0.0 Total weight: 0 Average weight: 0 Max weight: 0

Expected output Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5:

You entered: 123.4 56.0 98.0 174.0 215.8 Total weight: 667.2 Average weight: 133.44 Max weight: 215.8

TriskalJM
  • 2,393
  • 1
  • 19
  • 20
sag
  • 1
  • 2

4 Answers4

1

You are not taking the input from the user. Use Scanner or BufferedReader to get what the user is entering at the console.

Read this question for more details: How can I get the user input in Java?

Community
  • 1
  • 1
Pooja Arora
  • 574
  • 7
  • 19
0

You are not getting the output because you are not taking the input from the user. what you have to do is to get the input to a variable and add it to the array. To get the input from the console you can use Scanner. Below is the edited code. Try this, I think you can get the output.

import java.text.DecimalFormat;
import java.util.Scanner;

public class PeopleWeights {

   public static void main(String[] args) {
        DecimalFormat decFor = new DecimalFormat("0.00");

        Scanner sc = new Scanner(System.in);

        int i = 0;
        int n = 5;
        double arr[]=new double[n];

        for(i = 0; i < n; i++){
            System.out.println("Enter weight " + (i+1)+": ");
            double weight = Double.parseDouble(sc.nextLine());
            arr[i] = weight;
        }

        System.out.print("\nYou entered: ");

        for(i = 0; i < n; i++){
            System.out.print(arr[i] + " ");
        }

        System.out.println();

        double total = 0;
        double max = 0;

        for(i=0; i<n; i++){
            if(max < arr[i]){
                max = arr[i];
            }
            total = total + arr[i];
        }

        DecimalFormat df = new DecimalFormat("#.##");
        double average = total/n;
        System.out.println("Total weight: "+df.format(total));
        System.out.println("Average weight: "+df.format(average));
        System.out.println("Max weight: "+df.format(max));

    return;

    }
}
AnjuT
  • 166
  • 1
  • 4
  • 17
  • still getting a different error.Output 4, "Max weight:2".36 is highlighted with orange, Average weight: 145.3"6"- 6 is highlighted with orange EXPECTED OUTPUT Average weight: 145.3"5999999999999" the " " is highlighted. COMPARE OUTPUT 5: Your output Average weight: 145.3"6" - " " IS HIGHLIGHTED....expected output Average weight: 145.3"5999999999999" Max weight: 23"6.0" the " " are highlighted. Do you have a clue on how to fix it. – sag Apr 26 '17 at 06:56
  • I think the above code is working fine. Not clear with what you are saying. Can you put the error you are getting. – AnjuT Apr 26 '17 at 09:15
0

still getting a different error.Output 4, "Max weight:2".36 is highlighted with orange, Average weight: 145.3"6"- 6 is highlighted with orange EXPECTED OUTPUT Average weight: 145.3"5999999999999" the " " is highlighted. COMPARE OUTPUT 5: Your output Average weight: 145.3"6" - " " IS HIGHLIGHTED....expected output Average weight: 145.3"5999999999999" Max weight: 23"6.0" the " " are highlighted. Do you have a clue on how to fix it.

This is due to the use of DecimalFormat.

try:

import java.util.Scanner;

public class PeopleWeights {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int i = 0;
       int n = 5;
       double total = 0;
       double max = 0;
       double arr[]=new double[n];

       for(i = 0; i < n; i++){
           System.out.println("Enter weight " + (i+1)+": ");
           double weight = Double.parseDouble(sc.nextLine());
           arr[i] = weight;

           if(max < arr[i]){ 
              max = arr[i];
           }
           total = total + arr[i];
       }

       System.out.printf("%nYou entered: ");

       for(i = 0; i < n; i++){
           System.out.printf("%s ", arr[i]);
       }

       System.out.println();

       double average = total/n;
       System.out.printf("Total weight: %.1f%n", total);
       System.out.println("Average weight: " + average);
       System.out.printf("Max weight: %.1f%n" + max);

       return;
    }
}
Nick
  • 11
  • 3
0

This is what I used:

import java.util.Scanner;

/**
* Handles an array of peoples weights.
*/
public class PeopleWeights
{
// static variables that we can access in our static methods.
static final int WEIGHT_ARRAY_SIZE = 5;
static double[] weightsOfPeople = new double[WEIGHT_ARRAY_SIZE];
static double weightElement;
static double sum = 0.0;
static Scanner scnr = new Scanner(System.in);


public static void main(String[] args)
{
    GetWeights();
    WeightsSum();
    WeightsAverage();
    WeightsMax();
    return;
}

/**
 * Takes input for the weights and store them inside our array.
 */
public static void GetWeights()
{
    int i = 0; // Local count variable.

    // Take input for the weights and store them in our array.
    for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
    {
        System.out.println("Enter weight " + (i + 1) + ": ");
        weightElement = scnr.nextDouble();
        weightsOfPeople[i] = weightElement;
    }

    // Display what the user entered
    System.out.print("\nYou entered: ");
    for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
    {
        System.out.print(weightsOfPeople[i] + " ");
    }

    return;
}

/**
 * Gets the sum of our weights and then displays that sum.
 */
static void WeightsSum()
{
    int i = 0;

    for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
    {
        sum += weightsOfPeople[i];
    }

    System.out.println("\nTotal weight: " + sum);

    return;
}

/**
 * Gets the average of our weights and then displays that average.
 */
static void WeightsAverage()
{
    double averageWeight = 0.0;

    // Convert WEIGHT_ARRAY_SIZE to a double for good calculation.
    averageWeight = sum / (double)WEIGHT_ARRAY_SIZE;

    System.out.println("Average weight: " + averageWeight);

    return;
}

/**
 * Gets the max of our weights and then displays that max.
 */
static void WeightsMax()
{
    int i= 0;
    double maxWeight = 0.0;

    for(i = 0; i < WEIGHT_ARRAY_SIZE; i++)
    {
        if(weightsOfPeople[i] > maxWeight)
        {
            maxWeight = weightsOfPeople[i];
        }
    }

    System.out.println("Max weight: " + maxWeight);

    return;
}

}