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

Question? Can anyone tell me how to fix Space: An unexpected space was detected

ID: 3720382 • Letter: Q

Question

Question?

Can anyone tell me how to fix

Space: An unexpected space was detected in the output.

Here is my code:

import java.util.Scanner;

public class WeightAnalysis {

final static int NUM_WEIGHTS = 5;

public static void main(String[] args) {

  

Scanner keyboard = new Scanner(System.in);

double a[] = new double[NUM_WEIGHTS];

for(int i=0;i<a.length;i++) {

System.out.println("Enter weight "+(i+1)+":");

a[i] = keyboard.nextDouble();

}

System.out.println();
double sumWeight = 0,avgWeight = 0;

System.out.print("You entered weights: ");

for(int i=0;i<a.length;i++) {

System.out.print(a[i]+" ");

}  

sumWeight=getTotalWeight(a);

avgWeight = sumWeight/a.length;

System.out.println();
System.out.println();

System.out.printf( "Total weight: %.2f", sumWeight);


System.out.printf(" Average weight: %.2f", avgWeight);


System.out.printf( " Min weight: %.2f ", getMinWeight(a));

return;

}

/* Add method definition here */

public static double getMinWeight(double a[]) {

double min = a[0];

for(int i=0;i<a.length;i++) {

if(min>a[i]) {

min = a[i];

}

}

return min;

}

public static double getTotalWeight(double a[]) {

double sum = 0;

for(int i=0;i<a.length;i++) {

sum+=a[i];

}

return sum;

}

}

every time I get a bigger number I seen to have a error on my code see image for better undertanding.

PLEASE CORRECT MY CODE

:Compare output 96.6 212.2 Input 150.5 120.1 175 Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: Your output correctly starts with You entered weights: 96.6 212.2 150.5 120.1 175.0 Total weight: Average weight: Min weight: 754.40 150.88 96.60

Explanation / Answer

/*Can anyone tell me how to fix

Space: An unexpected space was detected in the output.

Here is my code:*/

import java.util.Scanner;

public class WeightAnalysis {

final static int NUM_WEIGHTS = 5;

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

double a[] = new double[NUM_WEIGHTS];

for (int i = 0; i < a.length; i++) {

System.out.println("Enter weight " + (i + 1) + ":");

a[i] = keyboard.nextDouble();

}

System.out.println();

double sumWeight = 0, avgWeight = 0;

System.out.print("You entered weights: ");

for (int i = 0; i < a.length; i++) {

System.out.print(a[i] + " ");

}

sumWeight = getTotalWeight(a);

avgWeight = sumWeight / a.length;

System.out.println();

System.out.println();

System.out.printf("Total weight: %.2f", Float.parseFloat((sumWeight+"").trim()));

System.out.printf(" Average weight: %.2f", Float.parseFloat((avgWeight+"").trim()));

System.out.printf(" Min weight: %.2f ", Float.parseFloat( (getMinWeight(a)+"").trim() ) );

return;

}

/* Add method definition here */

public static double getMinWeight(double a[]) {

double min = a[0];

for (int i = 0; i < a.length; i++) {

if (min > a[i]) {

min = a[i];

}

}

return min;

}

public static double getTotalWeight(double a[]) {

double sum = 0;

for (int i = 0; i < a.length; i++) {

sum += a[i];

}

return sum;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote