Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Help with program that takes in a list of numbers. (http://www.programmingforums.org/showthread.php?t=11363)

Yarvin Sep 20th, 2006 1:48 PM

Help with program that takes in a list of numbers.
 
I'm taking a Programming Concepts class this semester and the class is based on Java. Right now I'm working on the first part of the second assignment but my code won't run, or perhaps it will run but it doesn't do what I need it to do. Can someone look at my code and tell me what I did wrong? The program is supposed to gain a list of numbers through user input then output the average, max, and min of the list.

You don't have to fix it for me just kind of point me in the right direction.

Any help is greatly appreciated. :)

:

import java.util.*;

public class ProgProj3
{
        public static void main(String[] args)
        {
                final Scanner keyIn = new Scanner(System.in);
                double sum = 0;
                double nextNum = 0;
                double max = keyIn.nextDouble();
                double min = max;
                double next = 0;
                int amountOfNumbers = 0;
                String answer;
               
                do
                {
                        System.out.println();
                        System.out.println("Please enter in a list of numbers");
                        System.out.println("ending the list with a negative number.");
                       
                        while (next >= 0)
                        {
                                next = keyIn.nextDouble();
                                nextNum = next;
                                if (next > max)
                                        max = next;
                                if (next < min)
                                        min = next;
                               
                                sum = sum + nextNum;
                                amountOfNumbers++;
                                next = keyIn.nextDouble();
                                nextNum = next;
                        }
                       
                        if (amountOfNumbers > 0)
                                System.out.println("Your average is: " + (sum/amountOfNumbers));
                        else
                                System.out.println("No numbers to average!");
                        System.out.println("Your highest number is: " + max);
                        System.out.println("Your lowest number is: " + min);
                        System.out.println();
                        System.out.println("Would you like to try again?\n Type \"yes\" or \"no\".");
                        answer = keyIn.nextLine();                       
                } while (answer.equalsIgnoreCase("yes"));
               
        }
}


DaWei Sep 20th, 2006 2:13 PM

The input gathered in 1., below, will be overwritten by the input gathered in 2. Get rid of that second input. I didn't look at the code thoroughly, but that stands out.
:

2.                              next = keyIn.nextDouble();
                                nextNum = next;

                                if (next > max)
                                        max = next;
                                if (next < min)
                                        min = next;
                               
                                sum = sum + nextNum;
                                amountOfNumbers++;
1.                                next = keyIn.nextDouble();
                                nextNum = next;


Arevos Sep 20th, 2006 2:26 PM

Also, the definition for max:
:

  1. double max = keyIn.nextDouble();

This requests user input before you output the "Please enter in a list of numbers" blurb, which could be rather confusing for users.

Yarvin Sep 20th, 2006 5:25 PM

All right :) it all makes sense now....

Thank You!


All times are GMT -5. The time now is 1:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC