Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2008, 10:36 PM
Member
 
Join Date: Oct 2008
Posts: 1
trofyscarz is on a distinguished road
printing two smallest numbers from a series of numbers
Instructions: Write and debug a program by implementing a class called twoSmallest, which prompts the user to enter a series of numbers. Each number can have a fractional part. The user indicates the end of the list by typing a “z” as the next number. The program then prints out the two smallest numbers among the list that the user typed. The result for this problem should be a Java file called twoSmallest.java.

I am having no problem exiting the loop and printing the smallest number, I just cant figure out how to print the second smallest number.
Any suggestions?


import java.util.Scanner; // Needed for the Scanner class
public class TwoSmallest
{

public static void main(String[] args)
{

double smallest = Double.MAX_VALUE;
double secondsmallest ;

// Create a Scanner object for keyboard input.

Scanner sc = new Scanner(System.in);

// Display general instructions.

System.out.println("Enter a series of numbers.");

System.out.println("Press Z to exit the program.\n");

boolean exit = false;

while (!exit)

{

System.out.print("Enter a value ");

String input = sc.next();

if (input.equalsIgnoreCase("Z"))

exit = true;

else

{

double x = Double.parseDouble(input);

smallest = Math.min(smallest ,x);
}

}

System.out.println("Smallest number: " + smallest);

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-15-2008, 12:08 AM
Senior Member
 
Join Date: Sep 2008
Posts: 106
emceenugget is on a distinguished road
Two quick solutions I could think of.

1. You can take the easy route by inserting all inputs into a list, sorting it, and taking the first two values.

2. Obviously, you can keep track of two numbers instead of one in your loop. It only takes a little bit of thinking on how you would update those variables as you get each input.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-15-2008, 01:46 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
I just cant figure out how to print the second smallest number.
This should do it:
System.out.println("Second smallest= " + secondsmallest);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random numbers jithan Advanced Java 3 06-14-2008 04:04 PM
Prime numbers tercius New To Java 3 05-04-2008 08:05 AM
Compare 5 numbers Snowboardmylife New To Java 5 04-15-2008 09:04 PM
Printing Fibonacci Numbers Java Tip java.lang 0 04-09-2008 08:43 PM
Counting numbers up and down radio New To Java 3 12-01-2007 09:08 PM


All times are GMT +3. The time now is 10:18 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org