How do I add an Integer to another Integer?

S

Sebastian Stelzer

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>
 
J

Jeffrey Spoon

Sebastian Stelzer said:
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.
 
A

Alex Hunsley

Sebastian said:
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
 
J

John Davison

Jeffrey said:
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
 
J

Jeffrey Spoon

John Davison said:
Jeffrey said:
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.
 
G

George W. Cherry

John Davison said:
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
 
Joined
Apr 7, 2010
Messages
1
Reaction score
0
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:

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top