# Sortings three integers

Page 1 of 1

## 6 Replies - 35027 Views - Last Post: 19 March 2012 - 09:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=199195&amp;s=745dea2d88f31f3a7a4cf9053f0fdba9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 farmhaus

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 17-October 10

# Sortings three integers

Posted 09 November 2010 - 12:02 PM

Hello,

I have to write a method to display three numbers in increasing order. I think I have most of the code correct, but I cannot figure out what the error is.

```import java.util.Scanner;

public class Prog17 {
public static void main(String[] args) {

//Create a scanner
Scanner input = new Scanner(System.in);

//Prompt the user to enter three numbers, in double
System.out.print("Enter the first number: ");
double num1 = input.nextDouble();
System.out.print("Enter the second number: ");
double num2 = input.nextDouble();
System.out.print("Enter the third number: ");
double num3 = input.nextDouble();

System.out.println("The numbers in ascending order are:" + displaySortedNumbers(num1, num2, num3));
}

//Put the numbers in order
public static double displaySortedNumbers(double num1, double num2, double num3) {
if ( (num1 < num2) && (num1 < num3) ) {
System.out.print("The numbers in ascending order are:" + num1);
if (num2 < num3)
System.out.println(" " + num2 + " " + num3);
else
System.out.println(" " + num3 + " " + num2);
}
else if ( (num2 < num1) && (num2 < num3) ) {
System.out.print("The numbers in ascending order are:" + num2);
if (num1 < num3)
System.out.println(" " + num1 + " " + num3);
else
System.out.println(" " + num3 + " " + num1);
}
else if ( (num3 < num1) && (num3 < num2) ) {
System.out.print("The numbers in ascending order are:" + num3);
if (num1 < num2)
System.out.println(" " + num1 + " " + num2);
else
System.out.println(" " + num2 + " " + num1);
}
}
}

```

The error I am getting is:
```Prog17.java:46: missing return statement
}
^
1 error

```

Thank you for any help.

Is This A Good Question/Topic? 0

## Replies To: Sortings three integers

### #2 Deviara

Reputation: 6
• Posts: 69
• Joined: 02-October 10

## Re: Sortings three integers

Posted 09 November 2010 - 12:05 PM

public static double displaySortedNumbers(double num1, double num2, double num3)

You specify that it has a double return type, so it's expecting a return statement somewhere in your method that returns a double value.

This post has been edited by Deviara: 09 November 2010 - 12:06 PM

### #3 farmhaus

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 17-October 10

## Re: Sortings three integers

Posted 09 November 2010 - 12:20 PM

So instead of printing "The numbers... + num1" + num2 + num3, etc, should I return num1, then return num2, return num3?

### #4 Atspulgs

Reputation: 90
• Posts: 516
• Joined: 29-July 09

## Re: Sortings three integers

Posted 09 November 2010 - 12:23 PM

Id say just get rid of the double
You will have to handle the returning double if your return it afterwards
I dont think that this case requires that.
Just change double to void and it should be ok.

Oh just now noticed that youre calling the method from within system.out.print so yeah return would do it or you change double to void and call the method outside the the System.out.print.

This post has been edited by Atspulgs: 09 November 2010 - 12:25 PM

### #5 farmhaus

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 17-October 10

## Re: Sortings three integers

Posted 09 November 2010 - 12:30 PM

Actually, the method should be void instead of double. Thanks, Atspulgs.

I changed the return type to void and called the method outside System.out.print

Also deleted the extra System.out.print statements that weren't needed.

```import java.util.Scanner;

public class Prog17 {
public static void main(String[] args) {

//Create a scanner
Scanner input = new Scanner(System.in);

//Prompt the user to enter three numbers, in double
System.out.print("Enter the first number: ");
double num1 = input.nextDouble();
System.out.print("Enter the second number: ");
double num2 = input.nextDouble();
System.out.print("Enter the third number: ");
double num3 = input.nextDouble();

System.out.println("The numbers in ascending order are: ");
displaySortedNumbers(num1, num2, num3);
}

//Put the numbers in order
public static void displaySortedNumbers(double num1, double num2, double num3) {
if ( (num1 < num2) && (num1 < num3) ) {
System.out.print(num1);
if (num2 < num3)
System.out.println(" " + num2 + " " + num3);
else
System.out.println(" " + num3 + " " + num2);
}
else if ( (num2 < num1) && (num2 < num3) ) {
System.out.print(num2);
if (num1 < num3)
System.out.println(" " + num1 + " " + num3);
else
System.out.println(" " + num3 + " " + num1);
}
else if ( (num3 < num1) && (num3 < num2) ) {
System.out.print(num3);
if (num1 < num2)
System.out.println(" " + num1 + " " + num2);
else
System.out.println(" " + num2 + " " + num1);
}
}
}

```

That is the corrected code. Thank you both for your help.

This post has been edited by farmhaus: 09 November 2010 - 12:39 PM

### #6 hambonie

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 13-March 12

## Re: Sortings three integers

Posted 13 March 2012 - 02:38 PM

i am trying to write this code to use JOptionPane. this is what i have so far. but it does not work. please help

```import javax.swing.JOptionPane;
import java.util.Scanner;

public class Sort2 {
public static void main(String[] args) {

//Create a scanner
Scanner input = new Scanner(System.in);

//Prompt the user to enter three numbers, in double
//System.out.print("Enter the first number: ");
//double num1 = input.nextDouble();
//System.out.print("Enter the second number: ");
//double num2 = input.nextDouble();
//System.out.print("Enter the third number: ");
//double num3 = input.nextDouble();

//prompt user with joption
String num1String = JOptionPane.showInputDialog(null,
" Enter first integer: ");
int num1 = Integer.parseInt(num1String);
String num2String = JOptionPane.showInputDialog(null,
" Enter second integer: ");
int num2 = Integer.parseInt(num2String);
String num3String = JOptionPane.showInputDialog(null,
" Enter third integer: ");
int num3 = Integer.parseInt(num3String);

System.out.println("You entered:" + num1 + "" + num2 + "" + "" + num3 + "\nThe numbers in ascending order are: ");
displaySortedNumbers(num1, num2, num3);

//joption output

JOptionPane.showMessageDialog(null, "You entered:" + " " + num1 + " " + num2 +
" " + " " + num3 + "\nThe numbers in ascending order are: " );
displaySortedNumbers(num1, num2, num3);
}

//Put the numbers in order
public static void displaySortedNumbers(double num1, double num2, double num3) {
if ( (num1 < num2) && (num1 < num3) ) {
System.out.print(num1);
if (num2 < num3)
System.out.println(" " + num2 + " " + num3);
else
System.out.println(" " + num3 + " " + num2);
}
else if ( (num2 < num1) && (num2 < num3) ) {
System.out.print(num2);
if (num1 < num3)
System.out.println(" " + num1 + " " + num3);
else
System.out.println(" " + num3 + " " + num1);
}
else if ( (num3 < num1) && (num3 < num2) ) {
System.out.print(num3);
if (num1 < num2)
System.out.println(" " + num1 + " " + num2);
else
System.out.println(" " + num2 + " " + num1);
}
}
}

```

### #7 hambonie

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 13-March 12

## Re: Sortings three integers

Posted 19 March 2012 - 09:12 AM

hambonie, on 13 March 2012 - 02:38 PM, said:

i am trying to write this code to use JOptionPane. this is what i have so far. but it does not work. please help

```import javax.swing.JOptionPane;
import java.util.Scanner;

public class Sort2 {
public static void main(String[] args) {

//Create a scanner
Scanner input = new Scanner(System.in);

//Prompt the user to enter three numbers, in double
//System.out.print("Enter the first number: ");
//double num1 = input.nextDouble();
//System.out.print("Enter the second number: ");
//double num2 = input.nextDouble();
//System.out.print("Enter the third number: ");
//double num3 = input.nextDouble();

//prompt user with joption
String num1String = JOptionPane.showInputDialog(null,
" Enter first integer: ");
int num1 = Integer.parseInt(num1String);
String num2String = JOptionPane.showInputDialog(null,
" Enter second integer: ");
int num2 = Integer.parseInt(num2String);
String num3String = JOptionPane.showInputDialog(null,
" Enter third integer: ");
int num3 = Integer.parseInt(num3String);

System.out.println("You entered:" + num1 + "" + num2 + "" + "" + num3 + "\nThe numbers in ascending order are: ");
displaySortedNumbers(num1, num2, num3);

//joption output

JOptionPane.showMessageDialog(null, "You entered:" + " " + num1 + " " + num2 +
" " + " " + num3 + "\nThe numbers in ascending order are: " );
displaySortedNumbers(num1, num2, num3);
}

//Put the numbers in order
public static void displaySortedNumbers(double num1, double num2, double num3) {
if ( (num1 < num2) && (num1 < num3) ) {
System.out.print(num1);
if (num2 < num3)
System.out.println(" " + num2 + " " + num3);
else
System.out.println(" " + num3 + " " + num2);
}
else if ( (num2 < num1) && (num2 < num3) ) {
System.out.print(num2);
if (num1 < num3)
System.out.println(" " + num1 + " " + num3);
else
System.out.println(" " + num3 + " " + num1);
}
else if ( (num3 < num1) && (num3 < num2) ) {
System.out.print(num3);
if (num1 < num2)
System.out.println(" " + num1 + " " + num2);
else
System.out.println(" " + num2 + " " + num1);
}
}
}

```

This is the corrected code
```//csc151
//5.5 sorting integers with method
//by jeffrey m. taddei

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.String;

public class Sort2{

static Scanner console = new Scanner(System.in);
public static void main(String[]args){

int num1 = 0;
int num2 = 0;
int num3 = 0;

int UnsortInteger1 = num1;
int UnsortInteger2 = num2;
int UnsortInteger3 = num3;

//USER ENTERS
String SortInteger = JOptionPane.showInputDialog("Enter first number; " );
num1 = Integer.parseInt(SortInteger);

String SortInteger2 = JOptionPane.showInputDialog("Enter first number; " );
num2 = Integer.parseInt(SortInteger2);

String SortInteger3 = JOptionPane.showInputDialog("Enter first number; " );
num3 = Integer.parseInt(SortInteger3);

UnsortInteger1 = num1;
UnsortInteger2 = num2;
UnsortInteger3 = num3;

JOptionPane.showMessageDialog(null, "You entered: " + num1+" " + num2+" " + num3 +
"\nThe numbers sorted are: " + displaySortedNumbers(num1, num2, num3));

//MACHINE ENTERS realy big random numbers

num1 = (int) (Math.random()*10000 - 100);
num2 = (int) (Math.random()*10000 - 100);
num3 = (int) (Math.random()*10000 - 100);

UnsortInteger1 = num1;
UnsortInteger2 = num2;
UnsortInteger3 = num3;

JOptionPane.showMessageDialog(null, "Machine entered: " + num1+" " + num2+" " + num3 +
"\nThe numbers sorted are: " + displaySortedNumbers(num1, num2, num3));

//MACHINE ENTERS random negative
num1 = (int) (Math.random()*-1000);
num2 = (int) (Math.random()*-1000);
num3 = (int) (Math.random()*-1000);

UnsortInteger1 = num1;
UnsortInteger2 = num2;
UnsortInteger3 = num3;

JOptionPane.showMessageDialog(null, "You entered: " + num1+" " + num2+" "+ num3 +
"\nThe numbers sorted are: " + displaySortedNumbers(num1, num2, num3));

}
public static String displaySortedNumbers(int num1, int num2, int num3){
String result;
int temp = -111111;
if (num1 > num2){
temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3){
temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2){
temp = num1;
num1 = num2;
num2 = temp;
}
//return displaySortedNumbers;
result = ("\n" + num1 + " " + num2 + " " + num3);
return result;
}
}

```