PDA

View Full Version : Simple Java help finding the difference in two integer objects




cx300
Feb 6, 2009, 04:33 PM
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
Feb 6, 2009, 04:37 PM
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.


This is how code tag content looks like

cx300
Feb 6, 2009, 04:47 PM
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 --> return diffEnt1.compareTo(diffEnt2); is where i get errors
i originally had it so that it would say 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
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
Feb 6, 2009, 05:43 PM
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:


int d = Math.abs (i-j)


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

cx300
Feb 6, 2009, 05:47 PM
yes that would work, and i overlooked that...

but can you do that with integer objects?

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

the whole concept of objects is really confusing me

thanks again!

plinden
Feb 6, 2009, 05:52 PM
yes that would work, and i overlooked that...

but can you do that with integer objects?

Depends. What version of Java are you using?

If it 1.5 or greater, yes, the compiler handles converting Integer objects to int.

If 1.4 or earlier, take a look at the javadocs - http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html - in particular the methods such as intValue.

cx300
Feb 6, 2009, 05:54 PM
thank you so much!!

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

thanks again!!

eddietr
Feb 6, 2009, 05:56 PM
yes that would work, and i overlooked that...

but can you do that with integer objects?

would it be
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
Feb 6, 2009, 05:59 PM
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!