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: Why is my program not printing the highest and lowest numbers?

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is my program not printing the highest and lowest numbers?

    Hi! I'm a total beginner and I was wondering if someone can help me with this code.
    Last edited by momdubsnam; August 5th, 2020 at 09:37 AM.

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

    Default Re: Why is my program not printing the highest and lowest numbers?

    The test for the largest and smallest numbers should be inside of the do{}while loop, NOT after the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my program not printing the highest and lowest numbers?

    Thank you for the answer! However, when I try to do that, I get an error on the very last bracket saying "Syntax error, insert "while ( Expression ) ;" to complete DoStatement". I can't figure out what I'm doing wrong.

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

    Default Re: Why is my program not printing the highest and lowest numbers?

    Please post the code that gives the error
    and copy and paste here the full text of the error message.


    Check that the {}s are properly paired.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Why is my program not printing the highest and lowest numbers?

    messed up the brackets in my code
    Yes that is probably the problem.
    Not all the }s are inline and below the start of the line with the pairing {
    For example:
    				} else if (userNumber > largestNumber) {   //<<<<< Here is its pair
    					largestNumber = userNumber;
    			}     //<<<<<<< This should be beneath the start the line with the pairing {

    Move the noted } to the right so it pairs with the {

    What is this hanging { doing?
      		{   //<<<<<< What is this for?
    				if (userNumber < smallestNumber) {
    Remove that {
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jul 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my program not printing the highest and lowest numbers?

    Sorry, what do you mean with "to the right"? I've been trying but can't seem to get it right...

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

    Default Re: Why is my program not printing the highest and lowest numbers?

    what do you mean with "to the right"?
    By to the right I mean add spaces before the character so that it moves to the right
    For example:
       y
                 x  << this x is to right of the above y


    --- Update ---

    where exactly do you mean with "beneath the start line"?
    I said:
    below the start of the line with the pairing {
       if(test) {
       ^ this is the start of the code on the line with the {
       }  // this should be beneath the start of the line with the pairing {
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jul 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my program not printing the highest and lowest numbers?

    Thanks. This removes the error but it doesn't solve the problem unfortunately. It still prints the last input instead of the smallest and largest. Happy for any suggestions!

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

    Default Re: Why is my program not printing the highest and lowest numbers?

    Can you post the new code?
    Also add some comments about what the program is going to do with the input from the user. What steps will it take to find the smallest and the largest values?
    The comments should describe what the code is going to do.
    I use the comments to check that the code is doing what it is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Why is my program not printing the highest and lowest numbers?

    What should the program do with the number it reads from the user?

    What if you did this manually?
    Some one gives you values one at a time and you are required to keep the smallest one in one pile and the largest in another.
    Start in the middle (assume that you have gotten past the first few numbers). You get a value from the user. What do you do with that value?
    What do you look at and what decisions do you make?

    Later we'll go back and work on the first values.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Need some helping finding the lowest, highest, sum, and average.
    By rmeade in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 26th, 2018, 09:17 PM
  2. Sorting from lowest to highest issue
    By cb0688 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 24th, 2014, 07:34 AM
  3. Printing highest number of array. Can't find my error.
    By Praetorian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2013, 11:46 PM
  4. Replies: 7
    Last Post: November 18th, 2012, 05:17 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM