# Help With Nesting Simple If-Else Loops

• October 10th, 2013, 01:51 PM
Sephyncloud
Help With Nesting Simple If-Else Loops
Good morning everyone. ~o) 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! :)>-

Code Java:

```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); }   } }```
• October 10th, 2013, 02:14 PM
avinandan012
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.
• October 10th, 2013, 02:25 PM
Sephyncloud
Re: Help With Nesting Simple If-Else Loops
Quote:

Originally Posted by avinandan012
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.
• October 10th, 2013, 02:55 PM
avinandan012
Re: Help With Nesting Simple If-Else Loops
Code :

`...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
Code :

`input.close();`
• October 10th, 2013, 03:04 PM
copeg
Re: Help With Nesting Simple If-Else Loops
@avinandan012, welcome to the forums. Please read the forum rules and the following:

http://www.javaprogrammingforums.com...n-feeding.html

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
• October 10th, 2013, 03:14 PM
Sephyncloud
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
http://www.javaprogrammingforums.com...n-feeding.html
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.
• October 10th, 2013, 03:22 PM
copeg
Re: Help With Nesting Simple If-Else Loops
Quote:

Originally Posted by Sephyncloud
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.
• October 10th, 2013, 03:48 PM
GoodbyeWorld
Re: Help With Nesting Simple If-Else Loops
if ((num1 < num2) && (num1 < num3));

That ; after the if statement seems kind of silly.
• October 10th, 2013, 05:56 PM
Junky
Re: Help With Nesting Simple If-Else Loops
As well as concatenating variables to empty Strings.