# Help three integer inputs

• (2 Pages)
• 1
• 2

## 15 Replies - 6487 Views - Last Post: 03 January 2011 - 03:53 PMRate 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=208020&amp;s=740fa55b03c5f1f173bdd56cdb981342&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tay619

Reputation: 0
• Posts: 3
• Joined: 02-January 11

# Help three integer inputs

Posted 02 January 2011 - 07:19 PM

Can anybody help me understand how to write this code?

Write a program that reads three integer inputs into variables. Display the input values in both the ordered entered and in sorted order. This program should sort the numbers so that value1 <= value2 <= value3. Do not use any sort functions to do this, use if conditionals.
Is This A Good Question/Topic? 0

## Replies To: Help three integer inputs

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help three integer inputs

Posted 02 January 2011 - 07:26 PM

What code have you produced so far ?
[rules][/rules]

The Scanner class is what you are looking for

### #3 Jimmy.Li

Reputation: 15
• Posts: 57
• Joined: 12-October 10

## Re: Help three integer inputs

Posted 02 January 2011 - 07:40 PM

Well, I think the point of this excersice is to figur out the math behind the functions. If you really want to know, then just google "sort algorithms" and you'll get about a trillion results.
For the input part, you can use the Scanner tool.
```import java.util.Scanner;
...
public static void main( String[] args ){
Scanner in = new Scanner( System.in );

for( int i=0; i<AMT_TO_READ; i++ ){
n[i] = in.nextInt();
}
}

```

### #4 tay619

Reputation: 0
• Posts: 3
• Joined: 02-January 11

## Re: Help three integer inputs

Posted 02 January 2011 - 07:51 PM

```import javax.swing.JOptionPane;
public class Weekfour{
public static void main(String[] args) {

String num1 = JOptionPane.showInputDialog("Enter Integer : ");

String num2 = JOptionPane.showInputDialog("Enter Integer : ");

String num3 = JOptionPane.showInputDialog("Enter Integer : ");

int num1, num2, num3;
Scanner in = new Scanner(System.in);
int num1 = in.nextInt(); // First number
int num2 = in.nextInt(); // Second number
int num3 = in.nextInt();// Third number

int temp1 = num1;
int temp2 = num2;
//now begin the swap
num1 = num3; //num1 now has num3 value
num2 = temp1; //num2 holds original num1
num3 = temp2;//num3 holds original num2

}

```

Here is what I have so far, I really having a hard time understanding the java coding.

Enter the first of three integers: 3
Enter the second of three integers: 2
Enter the third of three integers: 0

The is the order the integers were originally entered: 3, 2, and 0.

The is the order of the intergers after they were sorted: 0, 2, and 3.

This post has been edited by pbl: 02 January 2011 - 08:12 PM
Reason for edit:: Code tags fixed

### #5 Jimmy.Li

Reputation: 15
• Posts: 57
• Joined: 12-October 10

## Re: Help three integer inputs

Posted 02 January 2011 - 08:07 PM

Okay. Well, to begin with, I saw no conditional statements in your code (such as if, while, and for). Have you learned them yet? Here's what I would do...
```public static void main(String[] args){
//Read numbers into an array called "n".
Scanner in = new Scanner( System.in );
int[] n = new int[AMT_TO_READ]; //Swap amount to read for the amount of number you want to read (or define it as a constant, take it as an arguemnt (using args[0]) or whatever)
for( int i=0; i<AMT_TO_READ; i++ ){
n[i] = in.nextInt();
}

//Compare three numbers using "Bubble sort (The easiest method)"
/**
Bubble sorting means a value gets swapped up the ranks until no one else is bigger than it.
Doing this for every value will ensure that no one has a value bigger than itself, and thus sorted!
To expalin the for-loop: it is a loop that executes it's body defined number of times, three elements must be defined...
for( counter ; break-condition; counter-change ) ;

counter - The thing that keeps track of the number of times executed.
break-condition - When the execution should stop, like when the counter reaches a certain number. (In most cases, the length of an array, so that all array elements are processed)
counter-change - How the counter keeps track of the execution (i.e How it changes after each execution)

Note: The for-loop checks the condition first before entering the body (execution code), so if the condition is not met at the first entering of the loop, it will not execute at all.
*/
for( int i=0; i<n.length; i++ ){
//If the first is larger than the second, swap them.
for( int k=0; k<(n.length-i); k++ ){
if( n[i] > n[i+k] ){
int first = n[i+k];
n[i] = n[i+k]; //First is now second.
n[i+1] = first; //Second is now first.
}
}
}
}
```

Hope you learned something...
Edit: Sorry, I think I misread your question! (I don't know how, I must be really tierd)

This post has been edited by Jimmy.Li: 02 January 2011 - 08:34 PM

### #6 tay619

Reputation: 0
• Posts: 3
• Joined: 02-January 11

## Re: Help three integer inputs

Posted 02 January 2011 - 08:14 PM

I've learned the If-else statement, thanks for providing some insight.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help three integer inputs

Posted 02 January 2011 - 08:22 PM

```// here you declare num1, num2, num3 as String variable

String num1 = JOptionPane.showInputDialog("Enter Integer : ");
String num2 = JOptionPane.showInputDialog("Enter Integer : ");
String num3 = JOptionPane.showInputDialog("Enter Integer : ");

// here you CANNOT change num1, num2 and num3 to be int
int num1, num2, num3;
Scanner in = new Scanner(System.in);
int num1 = in.nextInt(); // First number
int num2 = in.nextInt(); // Second number
int num3 = in.nextInt();// Third number

```

And make up your mind Are you using JOptionPane to input your data OR a Scanner ?
Either one bot not BOTH

Jimmy.Li, on 02 January 2011 - 09:07 PM, said:

Okay. Well, to begin with, I saw no conditional statements in your code (such as if, while, and for). Have you learned them yet? Here's what I would do...
```public static void main(String[] args){
//Read numbers into an array called "n".
Scanner in = new Scanner( System.in );
int[] n = new int[AMT_TO_READ]; //Swap amount to read for the amount of number you want to read (or define it as a constant, take it as an arguemnt (using args[0]) or whatever)
for( int i=0; i<AMT_TO_READ; i++ ){
n[i] = in.nextInt();
}

//Compare three numbers using "Bubble sort (The easiest method)"
for( int i=0; i<n.length; i++ ){
//If the first is larger than the second, swap them.
for( int k=0; k<(n.length-i); k++ ){
if( n[i] > n[i+k] ){
int first = n[i+k];
n[i] = n[i+k]; //First is now second.
n[i+1] = first; //Second is now first.
}
}
}
}

```

Hope you learned something...
Edit: Sorry, I think I misread your question! (I don't know how, I must be really tierd)

Java has a Math.max() method that works with int, float, double and long
No need to re-invent the wheel

### #8 Jimmy.Li

Reputation: 15
• Posts: 57
• Joined: 12-October 10

## Re: Help three integer inputs

Posted 02 January 2011 - 08:29 PM

Sorry, I updated my original post to fix some misunderstandings and errors on my part. I was thinking of another problem I encountered recently. (Silly me )

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help three integer inputs

Posted 02 January 2011 - 08:36 PM

Jimmy.Li, on 02 January 2011 - 09:29 PM, said:

Sorry, I updated my original post to fix some misunderstandings and errors on my part. I was thinking of another problem I encountered recently. (Silly me )

Admitting an error have this error half forgiven
Not the first time you make that mistake
And as I have already said, we are dealing with newbies and your wrong postings generate a lot of questions if your cut & pasted code does not work
There are no reason to rush... you will never reach anyhow macosxnerd101 hit parade of the most poster (I mean with as inteligent replies as his )

This post has been edited by pbl: 02 January 2011 - 08:42 PM

### #10 Jimmy.Li

Reputation: 15
• Posts: 57
• Joined: 12-October 10

## Re: Help three integer inputs

Posted 02 January 2011 - 08:49 PM

pbl, on 02 January 2011 - 07:36 PM, said:

And as I have already said, we are dealing with newbies and your wrong postings generate a lot of questions if your cut & pasted code does not work
There are no reason to rush... you will never reach anyhow macosxnerd101 hit parade of the most poster

I don't belive in cut & paste, so I try to put a few errors in them (Okay, obviously not true - but I really don't like cut & paste of unread code). You are right also on your other statement, I probably won't beat anyone in the most posts (As I don't have time or permission to stay online the entire day, sadly). It's just that I've always been a sit in a dark room and code type of guy and I decided to take a break from my own mad projects and help others for a change. But I probably won't be posting much longer though as the winter break end . School will consume most of my time again.

P.S I do not rush my code, but I don't revise it until it's golden and without syntax error - that's the compiler's job. XD

Edit: I think this is completely off-topic.

This post has been edited by Jimmy.Li: 02 January 2011 - 08:52 PM

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help three integer inputs

Posted 02 January 2011 - 08:53 PM

Jimmy.Li, on 02 January 2011 - 09:49 PM, said:

P.S I do not rush my code, but I don't revise it until it's golden and without syntax error - that's the compiler's job. XD

And so, for newbies, we are stucked with your innacurate replies !!! -1 and well desserved

Reputation:

## Re: Help three integer inputs

Posted 02 January 2011 - 09:05 PM

Oh well, how does a newbie learn if they just copy & paste perfect code?

### #13 LynnL

Reputation: 21
• Posts: 109
• Joined: 13-April 09

## Re: Help three integer inputs

Posted 02 January 2011 - 09:08 PM

Jimmy_Li, on 02 January 2011 - 08:05 PM, said:

Oh well, how does a newbie learn if they just copy & paste perfect code?

Really poor mentality: If it is innacurate code they will never learn
This is why it is better to suggest code rather than just post wrong code

This post has been edited by LynnL: 02 January 2011 - 09:09 PM

### #14 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help three integer inputs

Posted 02 January 2011 - 09:13 PM

LynnL, on 02 January 2011 - 10:08 PM, said:

Jimmy_Li, on 02 January 2011 - 08:05 PM, said:

Oh well, how does a newbie learn if they just copy & paste perfect code?

Really poor mentality: If it is innacurate code they will never learn
This is why it is better to suggest code rather than just post wrong code

Great !!! I am not the only one thinking that way. Tahnks LynnL

### #15 Jimmy.Li

Reputation: 15
• Posts: 57
• Joined: 12-October 10

## Re: Help three integer inputs

Posted 03 January 2011 - 02:15 PM

Well, we're all subjected to our own opinions. Although what I meant was while I try not to make mistakes in my code, I don't think it's nessesary to make the code perfect. To me, the code I post are only examples of what the reader should write, not the code they should use directly without at least modification and understanding.
Of course, no two people thinks the same.