Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: program that determines largest and smallest

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default program that determines largest and smallest

    Hi, I'm in a beginning java course and I had to write a program that determines the largest and smallest numbers entered by the user. My teacher wanted me to use -99 as the sentinel. I got the program to do what the teacher wanted with the inputs he told us to use however I have some issues with the smallest number if the number entered was more than 500 and the largest number less than zero so I was looking for some help with how to make my program work with all numbers. (keep in mind that I only have a fairly basic knowledge of java)

    import java.util.Scanner;
     
    public class LargestSmallest
    {
       public static void main (String[] args)
       {
          int userNumber;               //user entered number
          int largestSoFar = 0;         //Keeps track of largest number entered
          int smallestSoFar = 500;      //Keeps track of smallest number entered
          final int SENTINEL = -99;  
     
          Scanner keyboard =  new Scanner(System.in);
     
          //Get number from user
          System.out.print("Enter a positive whole number(enter -99 to terminate): ");
          userNumber = keyboard.nextInt();
     
          while (userNumber != SENTINEL)
          {
             if (userNumber > largestSoFar)
                largestSoFar = userNumber;
     
             if (userNumber < smallestSoFar)
                smallestSoFar = userNumber;
     
             System.out.print("Enter a positive whole number(enter -99 to terminate): ");
             userNumber = keyboard.nextInt();
          }  
     
          System.out.println();
          System.out.println("Largest number entered: " + largestSoFar);
          System.out.println("Smallest number entered: " + smallestSoFar);
       }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: program that determines largest and smallest

    Well obviously the initial values you assign to largest and smallest are wrong. Check out the Integer class.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: program that determines largest and smallest

    Look in the Math.* methods...

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: program that determines largest and smallest

    Sorry I am going to rework my solution so it isnt so revealing, I have a bad habit of doing the work for the student

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: program that determines largest and smallest

    your code so far
          int userNumber;               //user entered number
          int largestSoFar = 0;         //Keeps track of largest number entered
          int smallestSoFar = 500;      //Keeps track of smallest number entered
          final int SENTINEL = -99;  
     
          //Assume user puts int inside userNumber
     
          while (userNumber != SENTINEL)
          {
             if (userNumber > largestSoFar)
                largestSoFar = userNumber;
     
             if (userNumber < smallestSoFar)
                smallestSoFar = userNumber;
     
          //Assume user puts int inside userNumber
          }
    so your code does well to continue until the user flags it to stop, and is correct unless someone never enters a number larger then 0 or never smaller then 500.
    to fix this, unless your teacher gives you a default value, they have to start uninitialized.

    int largestSoFar;
    int smallestSoFar;

    this brings a new problem that you have to deal with, which is what if the first input is -99! your while loop never gets entered, so you need to think about how to handle that, it seems like a "special case" that an if statement might be up to solving on his own.

    Hope this helps,
    Jonathan

Similar Threads

  1. Replies: 3
    Last Post: October 19th, 2011, 02:17 PM
  2. Finding the smallest element and shifting....
    By DeadlySwordz in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 03:57 PM
  3. Output Largest and Smallest Integers using only If statements
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 2nd, 2011, 05:55 PM
  4. [SOLVED] My smallest and largest integers will not change.
    By toiletsauce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2011, 07:50 PM
  5. finding the largest object help
    By nickypass in forum Object Oriented Programming
    Replies: 4
    Last Post: October 16th, 2010, 05:48 PM