Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class PeopleWeights { public static void main(String args[]) { Scanner sc

ID: 3199756 • Letter: P

Question

public class PeopleWeights {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double m[]=new double[5];
double weight = 0, avg, max = 0;
int i;
for (i = 0; i < 5; i++)
{
System.out.println("Enter weight " + (i + 1) + ": ");

m[i] = sc.nextDouble();

}
System.out.println();

System.out.print("You entered: ");
for (i = 0; i < 5; i++)
{
System.out.print(" "+m[i]);
weight = weight + m[i];
}
for (i = 0; i < 5; i++)
{
if(m[i]>max)
max=m[i];
}

System.out.println(" ");

avg = weight / 5;
System.out.println("Total weight: " + weight);
System.out.println("Average weight: " + avg);
System.out.println("Max weight: " + max);
}
}

The above code is generating the following results with an extra space that I am not sure how to get ride of:

5: Compare output Output is nearly correct; but whitespace differs. See highlights below. Special character legend 236 89.5 Input 142 166.3 93 Enter weight 1: Enter weight 2: Enter weight 3 Enter weight 4: Enter weight5: Your output You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8 Average weight: 145.35999999999999 Max weight: 236.0 Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: Expected output You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8 Average weight: 145.35999999999999 Max weight: 236.0

Explanation / Answer

In line 17 where you've printed "You've entered: " you've added an extra space.
So it should be System.out.print("You entered:");

Therefore, your new code will be:

public class PeopleWeights {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double m[]=new double[5];
double weight = 0, avg, max = 0;
int i;
for (i = 0; i < 5; i++)
{
System.out.println("Enter weight " + (i + 1) + ": ");

m[i] = sc.nextDouble();

}
System.out.println();

System.out.print("You entered:");
for (i = 0; i < 5; i++)
{
System.out.print(" "+m[i]);
weight = weight + m[i];
}
for (i = 0; i < 5; i++)
{
if(m[i]>max)
max=m[i];
}

System.out.println(" ");

avg = weight / 5;
System.out.println("Total weight: " + weight);
System.out.println("Average weight: " + avg);
System.out.println("Max weight: " + max);
}
}


Execute it and it will work how you'd want it to.