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

Thread: Help With Nesting Simple If-Else Loops

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Help With Nesting Simple If-Else Loops

    Good morning everyone. After spending a few hours pouring through the textbook I have been given for this assignment, I still cannot get my code to work. I am almost positive it is an issue with the nesting of my If-Else statements. I am also having an issue with the code, as it outputs the numbers added together. When I run the code and enter the numbers (10,5,20), my output is the following:

    10
    5 20
    5
    1020
    20
    510

    The whole point of the program is to take the 3 numbers entered by the user(10,5,20), and output/sort them in ascending order (5,10,20).

    If you can point me in the right direction without being too vague, I would greatly appreciate it. Thank you and have a wonderful day!

    package sortthreeintegers;
     
    import java.util.Scanner;
     
    public class SortThreeIntegers
    {
     
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter your first number: ");
            int num1 = input.nextInt();
            System.out.print("Enter your second number: ");
            int num2 = input.nextInt();
            System.out.print("Enter your third number: ");
            int num3 = input.nextInt();
     
            //checking num1
     
            if ((num1 < num2) && (num1 < num3));
            {
                System.out.println("" + num1 + "");
     
            if (num2 < num3) {
                System.out.println(num2 + " " + num3);
             }
            else
                System.out.println(num3 + "" + num2);
            }  
     
            //checking num2
     
            if ((num2 < num1) && (num2 < num3));
            {
                System.out.println("" + num2 + "");
            }
            if (num1 < num3) {
                System.out.println(num1 + "" + num3);
            }
            else {
                System.out.println(num3 + "" + num1);
            }
     
            //checking num3
     
                    if ((num3 < num1) && (num3 < num2));
            {
                System.out.println("" + num3 + "");
            }
            if (num1 < num2) {
                System.out.println(num1 + "" + num2);
            }
            else {
                System.out.println(num2 + "" + num1);
            }
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help With Nesting Simple If-Else Loops

    why are you doing with only if else ??Any particular reason?
    You can use any sorting algorithm.

    The way you are printing is outright wrong. Store the values in order in an Array then print the array.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Nesting Simple If-Else Loops

    Quote Originally Posted by avinandan012 View Post
    why are you doing with only if else ??Any particular reason?
    You can use any sorting algorithm.

    The way you are printing is outright wrong. Store the values in order in an Array then print the array.
    We haven't learned about arrays yet, and therefore I cannot use them for this particular program.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help With Nesting Simple If-Else Loops

    ...removed spoonfed code


    --- Update ---

    Ohh there's another thing you're missing you didn't dissolve the scanner object.

    ALWAYS CLOSE ANY TYPE OF STREAM AFTER USE.

    add this at the end of main
    input.close();
    Last edited by copeg; October 10th, 2013 at 03:05 PM. Reason: Removed spoonfed code

  5. The Following User Says Thank You to avinandan012 For This Useful Post:

    Sephyncloud (October 10th, 2013)

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

    Default Re: Help With Nesting Simple If-Else Loops

    @avinandan012, welcome to the forums. Please read the forum rules and the following:

    The Problem with Spoon-feeding

    The post has been edited...I do wish the original poster makes an effort to tackle the problem by writing their own algorithm down on paper, helping to understand the problem solving aspect rather than copying and pasting the code for their homework (which is academic dishonesty). In more general terms, the removed code performed an algorithm which a) evaluates if a number is an 'extreme' (largest or smallest) b) if so, evaluate the remaining c) if not, evaluate the other pair to see which is the extreme the perform step b

  7. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    9
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help With Nesting Simple If-Else Loops

    To: avinandan012

    Thanks a lot for clearing up my nested statements for me! One thing that wasn't required for the assignment, but that I'd like to learn is: How would you output it correctly if they entered 2 or 3 of the same number? I'm not stressing over that, but I think that knowledge would be useful down the line. You've been a great help to me today, and thanks again!

    --- Update ---

    Quote Originally Posted by copeg View Post
    @avinandan012, please read the forum rules and the following:
    The Problem with Spoon-feeding
    The post has been edited...I do wish the original poster makes an effort to tackle the problem with an understanding rather than copying and pasting for their homework. In more general terms, the removed code a) Evaluate if a number is an 'extreme' (largest or smallest) b) if so, evaluate the remaining c) if not, evaluate the other pair to see which is the extreme and evaluate the remaining
    I did make an effort and you are an idiot for deleting the code the poster provided for me. (The damage has been done) I didn't simply copy and paste his code into mine. I took a few hours last night and this morning working on my code to get it into near working condition. The code that avinandan012 provided me helped me out a lot, but I didn't copy and paste it like you assume. This forum seems to be very unhelpful due to people like you who force people to be very vague and arrogant with their answers. Anyways, Thanks a lot Avinandan012, and NO THANKS to you copeg.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help With Nesting Simple If-Else Loops

    Quote Originally Posted by Sephyncloud View Post
    I did make an effort and you are an idiot for deleting the code the poster provided for me. (The damage has been done) I didn't simply copy and paste his code into mine. I took a few hours last night and this morning working on my code to get it into near working condition. The code that avinandan012 provided me helped me out a lot, but I didn't copy and paste it like you assume. This forum seems to be very unhelpful due to people like you who force people to be very vague and arrogant with their answers. Anyways, Thanks a lot Avinandan012, and NO THANKS to you copeg.
    Best of luck in your endeavors.

  9. #8
    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
    203
    My Mood
    Stressed
    Thanks
    22
    Thanked 35 Times in 35 Posts

    Default Re: Help With Nesting Simple If-Else Loops

    if ((num1 < num2) && (num1 < num3));


    That ; after the if statement seems kind of silly.

  10. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Help With Nesting Simple If-Else Loops

    As well as concatenating variables to empty Strings.
    Improving the world one idiot at a time!

Similar Threads

  1. Simple Shopping Cart..... not so simple
    By jpsider in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2012, 01:18 PM
  2. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  3. Error In Printing. Perhaps a nesting problem
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 23rd, 2011, 07:46 AM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 02:47 AM