Simple Java help finding the difference in two integer objects

Discussion in 'Mac Programming' started by cx300, Feb 6, 2009.

  1. macrumors member

    Joined:
    Sep 12, 2008
    Location:
    Clermont, FL
    #1
    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);
    }
     
  2. macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #2
    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
    
     
  3. thread starter macrumors member

    Joined:
    Sep 12, 2008
    Location:
    Clermont, FL
    #3
    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);
    					}
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    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?
     
  5. thread starter macrumors member

    Joined:
    Sep 12, 2008
    Location:
    Clermont, FL
    #5
    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!
     
  6. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    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.
     
  7. thread starter macrumors member

    Joined:
    Sep 12, 2008
    Location:
    Clermont, FL
    #7
    thank you so much!!

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

    thanks again!!
     
  8. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8
    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.
     
  9. thread starter macrumors member

    Joined:
    Sep 12, 2008
    Location:
    Clermont, FL
    #9
    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!
     

Share This Page