Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cx300

macrumors member
Original poster
Sep 12, 2008
61
0
Clermont, FL
I really need help, I have this project due in 6 hours... i needed to implement a simple menu system and one of the choices was to get two user defined integers and define them in integer objects. I cannot get how to do this!

below is the header of my code excluding the menu choices and the other 3 menus

this line --> return diffEnt1.compareTo(diffEnt2); is what I am having trouble with, i dont know what i need to be calling, or is there an easier way to subtract the two numbers?

I apologize as I am very new to this...
thanks in advance!!


""
import java.util.*;

class p2 {
public static void main(String args[]) {
int number;
Scanner scanner = new Scanner(System.in);

(LINE BREAKS......)

if (number == 4){
int diffEnt1, diffEnt2, diffOut;
diffEnt1 = new Integer(2);
diffEnt2 = new Integer(2);
System.out.println("This function will find the difference of two integers");
System.out.println("Please enter the first integer: ");
diffEnt1 = scanner.nextInt();
System.out.println("Please enter the second integer: ");
diffEnt2 = scanner.nextInt();
System.out.println();
System.out.println("You want to find the difference of " + diffEnt1 + " and " + diffEnt2);
return diffEnt1.compareTo(diffEnt2);
System.out.println("The difference of " + diffEnt1 + " and " + diffEnt2 + " is " + diffOut);
}
 

iShater

macrumors 604
Aug 13, 2002
7,026
470
Chicagoland
Please use the code tags (the ones that use the #) to wrap around your code.

And what exactly are you having trouble with on that line?

I already see a few issues with the code, but it is very hard to read.

Code:
This is how code tag content looks like
 

cx300

macrumors member
Original poster
Sep 12, 2008
61
0
Clermont, FL
sorry, i didnt know there were code tags...

I am having trouble comparing the two integer objects and finding the difference, as far as i know i didnt even define the object correctly

this line -->
Code:
return diffEnt1.compareTo(diffEnt2);
is where i get errors
i originally had it so that it would say
Code:
diffOut =  diffEnt1.compareTo(diffEnt2);
but that didnt work...

like i said im a freshman at the University of Florida going into computer hardware engineering and have interest in iphone development and figured i had to start somewhere... so here i am!

i really, really appreciate your help as I have not progressed in HOURS


EDIT: at the beginning of the code where it says "int number" that is used for determining the menu selection that is working
Code:
import java.util.*;								//Imports Util Package


class p2 {
  public static void main(String args[]) {
  		int number;
  		
 Scanner scanner = new Scanner(System.in);		//Declares a Scanner
  		
(BREAKS IN THE CODE THAT ACTUALLY WORK)

if (number == 4){
	int diffEnt1, diffEnt2, diffOut;
	diffEnt1 = new Integer(2);
	diffEnt2 = new Integer(2);
		System.out.println("This function will find the difference of two integers");
		System.out.println("Please enter the first integer:		");
			diffEnt1 = scanner.nextInt();
		System.out.println("Please enter the second integer: 	");
			diffEnt2 = scanner.nextInt();
		System.out.println();
		System.out.println("You want to find the difference of " + diffEnt1 + " and " + diffEnt2);
		        return diffEnt1.compareTo(diffEnt2);
		System.out.println("The difference of " + diffEnt1 + " and " + diffEnt2 + " is " + diffOut);
					}
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
I may be misunderstand what you're trying to do here, but to find the difference between two integers you would just subtract one from the other.

So given integer i and j, the difference d would be:

Code:
int d = Math.abs (i-j)

Isn't that what you're trying to do?
 

cx300

macrumors member
Original poster
Sep 12, 2008
61
0
Clermont, FL
yes that would work, and i overlooked that...

but can you do that with integer objects?

would it be
Code:
diffOut = Math.abs(diffInt1 - diffInt2)
??

the whole concept of objects is really confusing me

thanks again!
 

cx300

macrumors member
Original poster
Sep 12, 2008
61
0
Clermont, FL
thank you so much!!

it works great, i guess i was just making it harder than it needed to be...

thanks again!!
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
yes that would work, and i overlooked that...

but can you do that with integer objects?

would it be
Code:
diffOut = Math.abs(diffInt1 - diffInt2)
??

You should try it, that's what learning is all about. :)

The compareTo method you were trying to use is something totally different. What it does is compare the value of one Integer to another and tell you if they are (a) the same value, or (b) which one is numerically larger.

If they are different, it will tell you which number is larger (or smaller) but not the difference between the two numbers.
 

cx300

macrumors member
Original poster
Sep 12, 2008
61
0
Clermont, FL
ah... ok.

again, guess i was jumping ahead... but im trying!

i didnt have me MBP on me at that second to "try it" but i did a few minutes later and it worked great


thank you all again!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.