Question

I cannot seem to find a single answer to this question. The output of this code...

I cannot seem to find a single answer to this question. The output of this code is You entered: 236.0 89.5 142.0 166.3 93.0 with a trailing space at the end of the number 93.0. These inputs are changed and always have a trailing space on the last number. Now, I know it's because I have System.out.print(m[i] + " "); with the " " creating the space in between each number. How do I write it so each number has a space in between but not a trailing space on the last number? This is currently in Java.

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.print("\nYou 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);
  
  
return;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

we have to make small change in the program to print as per requiements:

code:

import java.util.*;
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.print("\nYou entered: ");
for (i = 0; i < 5; i++)
{
if(i==4)       //for last value only this will br printed with only value
{
System.out.println(m[i]);       //this will print without trailing space;
weight = weight + m[i];
}
else
{
System.out.print(m[i] + " ");//this is printed for value and space between number
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);
  
  

}
}

OUTPUT:

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

Total weight: 726.8
Average weight: 145.35999999999999
Max weight: 236.0
screenshot:

Add a comment
Know the answer?
Add Answer to:
I cannot seem to find a single answer to this question. The output of this code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5.23 LAB: Warm up: People's weights (1) Prompt the user to enter five numbers, being five...

    5.23 LAB: Warm up: People's weights (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. (2 pts) Ex: 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 (2) Also output the total weight, by summing the array's elements....

  • 14.5 Prog 5: People's weights (arrays) JAVA (1) Prompt the user to enter five numbers, being...

    14.5 Prog 5: People's weights (arrays) JAVA (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. (2 pts) Ex: 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 (2) Also output the total weight, by summing the array's...

  • 5.18 Ch 5 Warm up: People's weights (Vectors) (C++) (1) Prompt the user to enter five...

    5.18 Ch 5 Warm up: People's weights (Vectors) (C++) (1) Prompt the user to enter five numbers, being five people's weights. Store the numbers in a vector of doubles. Output the vector's numbers on one line, each number followed by one space. (2 pts) Ex: 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 89.5 142 166.3 93 (2) Also output the total weight, by summing...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Hello, can someone please help me correct this code? I greatly appreciate it. 7.16 Ch 7...

    Hello, can someone please help me correct this code? I greatly appreciate it. 7.16 Ch 7 Warm up: People's weights (Lists) (Python 3) (1) Prompt the user to enter four numbers, each corresponding to a person's weight in pounds. Store all weights in a list. Output the list. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 176.0 Enter weight 4: 166.3 Weights: [236.0, 89.5, 176.0, 166.3] (2) Output the average of the list's elements...

  • Hi, I need some help with the following question. I need the answer in Python please....

    Hi, I need some help with the following question. I need the answer in Python please. 10.23 LAB: Warm up: People's weights (Lists) (1) Prompt the user to enter four numbers, each corresponding to a person's weight in pounds. Store all weights in a list. Output the list. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 176.0 Enter weight 4: 166.3 Weights: [236.0, 89.5, 176.0, 166.3] (2) Output the average of the list's elements...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 1. What is the output of each of the following code segments if the inputs are...

    1. What is the output of each of the following code segments if the inputs are as follows: 5 0 15 -5 2 Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”); int number = input.nextInt(); if(number <= 0) break; sum += number; } System.out.println(sum); Second Segment: Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”);...

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT