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 10 of 10

Thread: Program that determines the lagest and the smallest number

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program that determines the lagest and the smallest number

    [B]What is Program about?[/B]
     
    I am working on this program which will keep getting input from user until 0 is entered. when 0 is entered program 
    will show which was the largest and the smallest number of all the numbers entered.
     
    [B]What is the error?[/B]
     
    Program below works and when I enter 0 it shows the largest value but shows 0 as the smallest value. 
    Example: if i enter few values like 5, 10, 15 and than enter 0 to get the results, it shows the correct 
    largest number which in this case is 15 but shows 0 as the smallest number which in this case is 5. How can that be corrected? 
     
    import java.util.Scanner; 
      public class LargestAndSmallest
        {
        public static void main(String [] args)
        {
        int number; 
        int largeNumber = 0;
        int smallNumber = 0;
     
        Scanner s = new Scanner(System.in);
     
        System.out.println("Enter multiple numbers\n" +
                           "and enter 0 to get the result of\n"+
                           "largest and smallest numbers entered");
     
        System.out.print("Enter a number or 0 to end or get results: ");
        number = s.nextInt();
        while (number != 0)
        {
     
        System.out.print("Enter a number or 0 to end or get results: ");
        number = s.nextInt();
     
        if(number < smallNumber )
          smallNumber = number;
        if (number > largeNumber )
          largeNumber = number;
        }
        System.out.println(smallNumber);
        System.out.println(largeNumber);
        }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Program that determines the lagest and the smallest number

    shows 0 as the smallest value.
    When does the code check for the smallest value
    and when does it check for the 0 to exit the loop?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Program that determines the lagest and the smallest number

    You code should end immediately if they enter 0 if you want it to stop then. Otherwise, it's going to operate on 0, and make 0 the smallest number because it will be.

    Also, I might add that unless the user enters 0 right away, the value of that number that the user entered before the while loop will be immediately overwritten in the while loop.

    Also, I might add that since smallNumber is already initialized to 0 even before the loop, that, unless you enter a negative, the smallest value will be 0.

    Make smallNumber the largest possible int. (I think it's 2147483647. It can also be obtained by setting smallNumber to Integer.MAX_VALUE)
    The same thing could, in theory, happen for largest if you entered

    -20 -15 -10 -5 0 (to stop and not counting 0.) It would return 0 as the largest even though the largest was -5.

    Make largeNumber the smallest possible int (I think it's -2147483648. It can also be obtained by setting largeNumber to Integer.MIN_VALUE)

    If you want it to always end and NEVER count 0, then a simple if/else statement should work to make sure it doesn't run the code on 0 but just exits the while loop.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program that determines the lagest and the smallest number

    Quote Originally Posted by GoodbyeWorld View Post
    You code should end immediately if they enter 0 if you want it to stop then. Otherwise, it's going to operate on 0, and make 0 the smallest number because it will be.

    Also, I might add that unless the user enters 0 right away, the value of that number that the user entered before the while loop will be immediately overwritten in the while loop.

    Also, I might add that since smallNumber is already initialized to 0 even before the loop, that, unless you enter a negative, the smallest value will be 0.

    Make smallNumber the largest possible int. (I think it's 2147483647. It can also be obtained by setting smallNumber to Integer.MAX_VALUE)
    The same thing could, in theory, happen for largest if you entered

    -20 -15 -10 -5 0 (to stop and not counting 0.) It would return 0 as the largest even though the largest was -5.

    Make largeNumber the smallest possible int (I think it's -2147483648. It can also be obtained by setting largeNumber to Integer.MIN_VALUE)

    If you want it to always end and NEVER count 0, then a simple if/else statement should work to make sure it doesn't run the code on 0 but just exits the while loop.
    Is there any way of solving it without these Integer.MIN_VALUE and MIX_VALUE etc?

  5. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Program that determines the lagest and the smallest number

    Yes, but using them ensures that it won't get messed up if you enter a value low enough or high enough that it returns the largest number or the smallest number based on what you initialized smallNumber and largeNumber to.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program that determines the lagest and the smallest number

    some one replied me in other thread and told me to do whats written below but I can't figure that out. Can you?

    int smallNumber = 0;

    You are comparing to this value to find the smallest number, so unless you have some negative numbers, 0 will always be the smallest.

    I would set smallNumber to the first value entered by the user, then compare the other numbers to this.

    A quick fix would be to initialize smallNumber with a large number />/>/>. That is, a number that will always be larger than those entered by the user.

  7. #7
    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 the lagest and the smallest number

    Do you have a question or are you just posting the help you got in another forum? BTW thanks for wasting everyones time if you were getting help elsewhere.
    Improving the world one idiot at a time!

  8. #8
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program that determines the lagest and the smallest number

    Quote Originally Posted by Junky View Post
    Do you have a question or are you just posting the help you got in another forum? BTW thanks for wasting everyones time if you were getting help elsewhere.
    No problem. You are welcome spoon feeders.

  9. #9
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Program that determines the lagest and the smallest number

    Lol was that an insult? Confusing but funny.

    Anyway, smallNumber and largeNumber should be initialised or at least set with the first number entered by the user. Not some arbitrary large number. Use the actual data ffs.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Program that determines the lagest and the smallest number

    This thread has been cross posted here:

    http://www.dreamincode.net/forums/topic/332738-program-that-determines-the-lagest-and-the-smallest-number/

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  11. The Following User Says Thank You to copeg For This Useful Post:

    Kewish (October 30th, 2013)

Similar Threads

  1. Java code to find the smallest number from a two dimensional array
    By prabhat11 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 23rd, 2013, 06:54 PM
  2. program that determines largest and smallest
    By Reverie in forum Loops & Control Statements
    Replies: 4
    Last Post: February 26th, 2012, 08:41 PM
  3. Replies: 1
    Last Post: November 26th, 2011, 12:13 PM
  4. Replies: 3
    Last Post: October 19th, 2011, 02:17 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM