Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 20th, 2006, 1:48 PM   #1
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
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"));
		
	}
}
__________________
[ADM]art
Yarvin is offline   Reply With Quote
Old Sep 20th, 2006, 2:13 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 20th, 2006, 2:26 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Also, the definition for max:
Java Syntax (Toggle Plain Text)
  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.
Arevos is offline   Reply With Quote
Old Sep 20th, 2006, 5:25 PM   #4
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
All right it all makes sense now....

Thank You!
__________________
[ADM]art
Yarvin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list problems bl00dninja C++ 6 Feb 17th, 2008 11:30 AM
dev c++ software, template problem cairo C++ 11 Jun 2nd, 2006 1:42 PM
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
User-defined creatNode and deleteNode functions for a doubly-linked list jgs C 2 Apr 28th, 2005 9:53 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:57 AM.

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