Help three integer inputs

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6366 Views - Last Post: 03 January 2011 - 03:53 PM Rate Topic: -----

#1 tay619  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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
http://download.orac...il/Scanner.html
Was This Post Helpful? 0
  • +
  • -

#3 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • 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 ){
	//Read from standard commandline input.
	Scanner in = new Scanner( System.in );
	//The stoarge for your numbers
	int[] n = new int[AMT_TO_READ];

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


Was This Post Helpful? 1
  • +
  • -

#4 tay619  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • 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".
	//Read from standard commandline input.  
	Scanner in = new Scanner( System.in );  
	//The stoarge for your numbers 
	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

Was This Post Helpful? 0
  • +
  • -

#6 tay619  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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

View PostJimmy.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".
	//Read from standard commandline input.  
	Scanner in = new Scanner( System.in );  
	//The stoarge for your numbers 
	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
Was This Post Helpful? 0
  • +
  • -

#8 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • 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 :P )
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Help three integer inputs

Posted 02 January 2011 - 08:36 PM

View PostJimmy.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 :P )

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

Was This Post Helpful? 2
  • +
  • -

#10 Jimmy.Li  Icon User is offline

  • D.I.C Head

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

Re: Help three integer inputs

Posted 02 January 2011 - 08:49 PM

View Postpbl, 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 :P (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

Was This Post Helpful? -2
  • +
  • -

#11 pbl  Icon User is offline

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

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

Re: Help three integer inputs

Posted 02 January 2011 - 08:53 PM

View PostJimmy.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
Was This Post Helpful? 2
  • +
  • -

#12 Guest_Jimmy_Li*


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?
Was This Post Helpful? -2

#13 LynnL  Icon User is offline

  • D.I.C Head

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

Re: Help three integer inputs

Posted 02 January 2011 - 09:08 PM

View PostJimmy_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

Was This Post Helpful? 3
  • +
  • -

#14 pbl  Icon User is offline

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

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

Re: Help three integer inputs

Posted 02 January 2011 - 09:13 PM

View PostLynnL, on 02 January 2011 - 10:08 PM, said:

View PostJimmy_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
Was This Post Helpful? 0
  • +
  • -

#15 Jimmy.Li  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2