12 Replies - 7942 Views - Last Post: 03 March 2010 - 02:22 PM Rate Topic: -----

#1 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 256
  • Joined: 21-February 10

Largest and Smallest number program

Posted 03 March 2010 - 12:39 PM

I need help on how to display the smallest number input by the user. The largest number is displayed but the smallest number is always 0, no matter what numbers I put in.

  /* @ Author Brock Shelton
  * Date Created: March 3, 2010
  *Purpose: Make a program to ask the user to enter numbers
  * then the program will display the largest and smallest numbers.
  */
   import java.util.Scanner;

    public class LargeSmall
   {
	
	/**
 *
 * @ param the args default argument for the main method
 *
 */ 
	
       public static void main(String[] args)
      {
         final String ASK_2 = "Enter number or -99 to end the program. ";
         final String ASK_1 = "How many numbers will you enter?";
         final int ZERO = 0;
         final String SAY_1 = "The smallest number was ";
         final String SAY_2 = "The largest number was ";
         int numbers;
         int smallest = 0;
         int largest = 0;
         int count = 0;
         int val;
         int testNum = -99;
         int max;
         final String INVALID = "Please do not enter negative numbers";
			
			final String LOGO = "  +-++-++-++-++-++-++-+ \n" +
    		        			  "  |B||u||h||R||o||c||k| \n" +
      	      			  "  +-++-++-++-++-++-++-+";   // makes a logo
      
      
         final String WELCOME = "Author: Brock Shelton \n"
            		+ "Date Created: February 23, 2010 \n"
            		+ "Purpose: This program will ask you to enter numbers. \n "
            		+ " Then it will display the largest and smallest numbers you entered. \n"; // declares a constant
      
      
      
         Scanner keyboard = new Scanner(System.in);
      
         System.out.println(WELCOME + LOGO);
      
      
         System.out.println(ASK_1);
         max = keyboard.nextInt();
      
         if (max <= -1)
         {
            System.out.println(INVALID);
         }
      
      
      
         for (count = 0; count < max; count++)
         {		
         
            System.out.println(ASK_2);
            val = keyboard.nextInt(); 
         
            if (val == testNum)
               System.exit(ZERO);			
         
            if (val > largest)
            {
               largest = val;
            }
         
            if (val < smallest) 
            {
               smallest = val;
            }
         }
      
      
      
         System.out.printf(SAY_1 + smallest);
         System.out.println(SAY_2 + largest);
      
      }
   }




Is This A Good Question/Topic? 0
  • +

Replies To: Largest and Smallest number program

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 12:46 PM

You are repeatedly testing if the entered number is smaller than the value of smallest. You initialized smallest to be 0. So you are testing whether the number the user put in is less than 0. If you intialize smallest to be something ridiculous, like 9999999, think about what will happen. The first time through, the user puts in 4815. Is 4815 lower than smallest now? Yes, so it will be assigned to smallest. Then, for the rest of the program, it will operate the way you expect it to.
Was This Post Helpful? 0
  • +
  • -

#3 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 256
  • Joined: 21-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 12:50 PM

What if, for some reason, someone enters a value more than 999999?
Was This Post Helpful? 0
  • +
  • -

#4 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Largest and Smallest number program

Posted 03 March 2010 - 12:53 PM

BuhRock try this...

int j = Integer.MAX_VALUE; // this will be 2,147,483,647.
Was This Post Helpful? 0
  • +
  • -

#5 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 12:55 PM

View PostBuhRock, on 03 March 2010 - 11:50 AM, said:

What if, for some reason, someone enters a value more than 999999?

If you're really worried about that, you can just write a statement that automatically assigns the first number entered to smallest without testing it. This is actually what you should have done. When the first number is entered, it should be assigned to smallest and largest, since it is in fact the largest and smallest number entered to that point.

My previous suggestion was just an easy way to fix your program. :)
Was This Post Helpful? 1
  • +
  • -

#6 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 256
  • Joined: 21-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:05 PM

Oh ok, I see now. I forgot another problem I had. Could you explain how I would go about on how to display an error message if the user inputs any character other than numerical data?
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:07 PM

Uhgh, this is asked so often, I wrote a code snippet on it. Of course, since mine uses a JOptionPane, you will want to make some changes:

Largest:
http://www.dreaminco...snippet4707.htm

Smallest:
http://www.dreaminco...snippet4706.htm
Was This Post Helpful? 0
  • +
  • -

#8 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 256
  • Joined: 21-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:14 PM

That snipplet is way over my head, and I can never get it to compile.
Was This Post Helpful? 0
  • +
  • -

#9 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:17 PM

This is pretty easy - I can think of two easy ways to do it.

Method 1:
Loop through the string that was entered and use Character.isDigit() on each. If that method returns false, display your error message and take appropriate action.

Method 2:
//USER INPUTS NUMBER HERE
try {
  Integer.parseInt(input);
  //rest of code dealing with input number goes here
} catch (Exception e) {
  System.out.print("Your input was invalid.  Please input only numbers.");
}


In the above example, the program gets the user input, and first thing in the try block it tries to pull a numeric value out of it. If the entered data was numeric, you get the number they entered. If they entered anything that is not numeric, it creates an error at that point and shoots to the catch statement, ignoring everything after the line that created the error. This way, if the user gave numeric data, it runs normally. If they entered bad input, the program does nothing with the bad input and displays an error message.
Was This Post Helpful? 0
  • +
  • -

#10 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:22 PM

You can check if its a number, by forcing it into a number, and then catching the exception thrown if the conversion fails( which it will if there are alpha or punctuation characters(except the minus sign in front))
int number = Integer.MAX_VALUE;
try{
 number= Integer.parseInt( "inpputNumberHere" );
}catch( NumberFormatException e ){
 System.err.println("You need to enter a number!");
}


This post has been edited by SwiftStriker00: 03 March 2010 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#11 BuhRock  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 256
  • Joined: 21-February 10

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:40 PM

I got confused and what Im actually supposed to do is go through a while loop until the user enters -99. But when I enter -99, it displays -99 as the smallest number. If I use System.exit(0) then it skips my output.

  /* @author Brock Shelton
  * Date Created: March 3, 2010
  *Purpose: Make a program to ask the user to enter numbers
  * then the program will display the largest and smallest numbers.
  */
   import java.util.Scanner;

    public class LargeSmall
   {
	
	/**
 *
 * @ param the args default argument for the main method
 *
 */ 
	
       public static void main(String[] args)
      {
         final String ASK_2 = "Enter number or -99 to end the program. ";
         final String ASK_1 = "How many numbers will you enter? You can enter";
         final int ZERO = 0;
         final String SAY_1 = "The smallest number was ";
         final String SAY_2 = "The largest number was ";
         int numbers;
			char char1 = 'a';
			char char2 = 'z';
         int smallest = Integer.MAX_VALUE;
         int largest = 0;
         int count = 0;
         int val;
         int testNum = -99;
         int max;
         final String INVALID = "Please do not enter negative numbers";
			
			final String LOGO = "  +-++-++-++-++-++-++-+ \n" +
    		        			  "  |B||u||h||R||o||c||k| \n" +
      	      			  "  +-++-++-++-++-++-++-+";   // makes a logo
      
      
         final String WELCOME = "Author: Brock Shelton \n"
            		+ "Date Created: February 23, 2010 \n"
            		+ "Purpose: This program will ask you to enter numbers. \n "
            		+ " Then it will display the largest and smallest numbers you entered. \n"; // declares a constant
      
      
      
         Scanner keyboard = new Scanner(System.in);
      
         System.out.println(WELCOME + LOGO);
      
      
         System.out.println(ASK_2);
         val = keyboard.nextInt();
			
			smallest = val;
			largest = val;
			
      
    		while ( val != -99)
			{
	
       	     System.out.println(ASK_2);
        	    val = keyboard.nextInt(); 
				 			
         
           	 if (val > largest)
            	{
             	  largest = val;
            	}
         
            	if (val < smallest) 
            	{
             	  smallest = val;
            	}
				   
			}
      
				// OutPut //
      
      
         System.out.println(SAY_1 + smallest);
         System.out.println(SAY_2 + largest);
      
      }
   }


Was This Post Helpful? 0
  • +
  • -

#12 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Largest and Smallest number program

Posted 03 March 2010 - 01:49 PM

Before your min and max tests...
You can test for -99 and use the command break; instead of system exits, this should break you out of your loop

if(val == 99)
break;
Was This Post Helpful? 0
  • +
  • -

#13 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Largest and Smallest number program

Posted 03 March 2010 - 02:22 PM

You can also evaluate the input as a part of the while conditional, and end if its -99
int min =Integer.MIN_VALUE, max = Integer.MAX_VALUE;
		Scanner keyboard = new Scanner(System.in);
		boolean firstTime = true;
		String input = "";
		while( !((input=keyboard.nextLine()).equals("-99")) ){
		  int number;
		  try{
			input.trim();
			number= Integer.parseInt( input);
			if( firstTime ){
				max = number;
				firstTime = false;
			}else{
				if( number < max ){
					min = number;
				}else{
					max = number;
				}
			}
			
		  }catch( NumberFormatException e ){
			System.err.println("You need to enter a number!");
		  }
		}
		System.out.println( "min: " + min );
		System.out.println( "max: " + max );


This post has been edited by SwiftStriker00: 03 March 2010 - 02:23 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1