How do I add an Integer to another Integer?

Discussion in 'Java' started by Sebastian Stelzer, Oct 14, 2004.

  1. Hi!
    I have a question.
    How can I add an Integer to another Integer?
    I ask, because the Java compiler printed an error message to the screen,
    while I was compiling my source file:

    <output>
    calcit2004.java:77: operator + cannot be applied to
    java.lang.Integer,java.lang.Integer
    iTmp3 = String.valueOf(iTmp1 + iTmp2);
    ^
    </output>

    Here is the line 77:
    <code>
    String iTmp3 = new String();
    Integer iTmp1 = new Integer(zahl1);
    Integer iTmp2 = new Integer(zahl2);
    iTmp3 = String.valueOf(iTmp1 + iTmp2);
    </code>


    --
    Java rockz!
     
    Sebastian Stelzer, Oct 14, 2004
    #1
    1. Advertising

  2. In message <ckmnuc$83c$02$-online.com>, Sebastian Stelzer
    <> writes
    >Hi!
    >I have a question.
    >How can I add an Integer to another Integer?
    >I ask, because the Java compiler printed an error message to the screen,
    >while I was compiling my source file:
    >
    ><output>
    >calcit2004.java:77: operator + cannot be applied to
    >java.lang.Integer,java.lang.Integer
    > iTmp3 = String.valueOf(iTmp1 + iTmp2);
    > ^
    ></output>
    >
    >Here is the line 77:
    ><code>
    >String iTmp3 = new String();
    >Integer iTmp1 = new Integer(zahl1);
    >Integer iTmp2 = new Integer(zahl2);
    >iTmp3 = String.valueOf(iTmp1 + iTmp2);
    ></code>
    >
    >


    Are zahl1 and zahl2 Strings or ints?

    Anyway you're not adding ints there, you're trying to "add" two
    references to Integer objects.

    AFAIK + either concatenates references using toString() or adds
    primitives mathematically. To do the latter get the primitive int value:

    iTmp3 = String.valueOf(iTmp1.intValue() + iTmp2.intValue());

    The Integer Objects is like a wrapper for a primitive int, it isn't an
    int itself so you can't just add two Integer Objects together.




    --
    Jeffrey Spoon
     
    Jeffrey Spoon, Oct 14, 2004
    #2
    1. Advertising

  3. Sebastian Stelzer

    Alex Hunsley Guest

    Sebastian Stelzer wrote:
    > Hi!
    > I have a question.
    > How can I add an Integer to another Integer?
    > I ask, because the Java compiler printed an error message to the screen,
    > while I was compiling my source file:
    >


    The Java API docs are really useful for answering questions like this.

    Please post beginner questions like this to comp.lang.java.help.
    And be aware that newsgroup posts take time to appear - so please don't
    repost the same message if the first attempt doesn't immediately appear!

    alex
     
    Alex Hunsley, Oct 15, 2004
    #3
  4. Sebastian Stelzer

    John Davison Guest

    Jeffrey Spoon wrote:

    > Anyway you're not adding ints there, you're trying to "add" two
    > references to Integer objects.
    >
    > AFAIK + either concatenates references using toString() or adds
    > primitives mathematically. To do the latter get the primitive int value:
    >
    > iTmp3 = String.valueOf(iTmp1.intValue() + iTmp2.intValue());
    >
    > The Integer Objects is like a wrapper for a primitive int, it isn't an
    > int itself so you can't just add two Integer Objects together.


    Unless you are using 1.5 in which case autoboxing would allow this.

    public class test {
    public static void main(String[] args) {
    Integer i = new Integer(2);
    Integer o = new Integer(2);
    System.out.println(i + o);
    }
    }

    John Davison
    Compass Engineering Group
     
    John Davison, Oct 15, 2004
    #4
  5. In message <>, John Davison
    <> writes
    >Jeffrey Spoon wrote:
    >
    >> Anyway you're not adding ints there, you're trying to "add" two
    >>references to Integer objects.
    >> AFAIK + either concatenates references using toString() or adds
    >>primitives mathematically. To do the latter get the primitive int value:
    >> iTmp3 = String.valueOf(iTmp1.intValue() + iTmp2.intValue());
    >> The Integer Objects is like a wrapper for a primitive int, it isn't
    >>an int itself so you can't just add two Integer Objects together.

    >
    >Unless you are using 1.5 in which case autoboxing would allow this.
    >
    >public class test {
    > public static void main(String[] args) {
    > Integer i = new Integer(2);
    > Integer o = new Integer(2);
    > System.out.println(i + o);
    > }
    >}


    That looks much neater. I might try 1.5 some time.



    --
    Jeffrey Spoon
     
    Jeffrey Spoon, Oct 15, 2004
    #5
  6. "John Davison" <> wrote in message
    news:...
    > Jeffrey Spoon wrote:
    >
    >> Anyway you're not adding ints there, you're trying to "add" two
    >> references to Integer objects.
    >>
    >> AFAIK + either concatenates references using toString() or adds
    >> primitives mathematically. To do the latter get the primitive int value:
    >>
    >> iTmp3 = String.valueOf(iTmp1.intValue() + iTmp2.intValue());
    >>
    >> The Integer Objects is like a wrapper for a primitive int, it isn't an
    >> int itself so you can't just add two Integer Objects together.

    >
    > Unless you are using 1.5 in which case autoboxing would allow this.


    Errr. Auto-UNboxing. : o )

    > public class test {
    > public static void main(String[] args) {
    > Integer i = new Integer(2);
    > Integer o = new Integer(2);
    > System.out.println(i + o);
    > }
    > }
    >
    > John Davison
    > Compass Engineering Group
     
    George W. Cherry, Oct 16, 2004
    #6
  7. Sebastian Stelzer

    JavaBean2010

    Joined:
    Apr 7, 2010
    Messages:
    1
    NetBeans-Java Help! Adding integers

    my problem is causing me alot of anger and confusion-

    I want to be-able to declare an int that will hold a value input by the user using the Scanner Utility which can then be continuously added upon through the user input.nextInt . With the code ive created all I get is the same starting balance but with new inputs that I input. This is what my code looks like:

    Code:
    public static void problem1()
    {
        Scanner input = new Scanner(System.in);
    
               System.out.printf( "Enter A Balance :\n");
               int acc = 59451; // the users account number
               int acc1; // the users attempt at their account number
               int bal = input.nextInt();
               int chks; // users check withdrawls
               int depst; // users deposit ammount
               int total; // the sum/remainder of bal - chks or bal + depst
               
    
    	   System.out.println("In problem # 1");
    	   System.out.print("\n");
                    System.out.print("Enter your Account# :\n");
                    acc1 = input.nextInt();
    
               if ( acc1 == acc )
    
                        System.out.printf( "Welcome to your Account!\n"+"\n"+
                                "Your Account Balance is :%d\n",bal);
    
               else System.out.print("Sorry your pin is incorrect\n");
               if (acc1 == acc )
               {
               int inN;
    		do
    		{
    		 BankMenu();
    		 inN = getN(input);
    		 switch(inN)
    		 {
    		  case 1:
    
    			  System.out.println( "Enter Check Ammount :\n");
                              chks = input.nextInt();
                              total = bal - chks;
    
                              if ( bal < chks )
                                  System.out.printf( "\nYou've over drawn!\n " +
                                          "Your account will be charged the remainder + a $12 Fee\n" +"\n Your New Balance is :%d\n",total-12);
    
                              else System.out.printf( "Your New Balance is :%d\n",total);
    			  break;
    		  case 2:
    			  System.out.println( "Enter Deposit Ammount :" );
                              depst = input.nextInt();
                              total = bal + depst;
    
                             System.out.printf( "Your New Balance is :%d\n",total);
    			  break;
                      default:
    			  def(inN);
    		 }
    		}while(inN<3);
               }
    }
    Take for instance - I have

    total = bal - chks the end result would then need to be put into a new placeholder so that i could countinue to add or subtract upon that specific sum or difference.


    PLEASE HELP.
     
    Last edited: Apr 7, 2010
    JavaBean2010, Apr 7, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,957
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    511
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    497
    Yu SONG
    Oct 15, 2004
  4. Johannes Zellner
    Replies:
    22
    Views:
    3,614
  5. les ander
    Replies:
    4
    Views:
    887
    wes weston
    Oct 5, 2004
Loading...

Share This Page