• Post Reply Bookmark Topic Watch Topic
  • New Topic

arithmetic operators  RSS feed

 
Adrienne Peck
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do arithmetic operators work in formulas using variables? i know that to multiply two primitives, we would do the following:

x=x*2

however, with a formula that has a string to be multiplied by 100 to produce an integer (Integer.parseInt((cprat.Month_In_Term)*"100")/Term), I get the following error:

'the operator * is undefined for the argument type java.lang.string.'

the java.lang.* import has been added to the class. would someone please tell me what's wrong with this code?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forget about java for a moment, and think arithmatic. how would you multiply "fred" times "blue"?

you can't. multiplication is defined on numbers.

now, in your code, your almost there. you have a string "cprat.Month_In_Term" (i assume that is a String). you then convert it to an int, using the Integer.parseInt() method.

so, what is ultimatly there on the Left-hand-side of your expression is an int.

you then try to multiply that by a String.

why?

why don't you just multiply it by 100?

(Integer.parseInt((cprat.Month_In_Term)*100)/Term)
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other languages, it is possible to define how arithmetic operators like *+-/ and non-arithmetic ones like [] should behave when applied to particular (combinations of) objects. You can certainly do that in C++; it's called "operator overloading". Therefore, the original poster's question is not a stupid one.

When used well in C++, operator overloading can do some nice stuff. The classic example is if you have a class representing a complex number. In C++, you can define how the arithmetic operators should behave for complex numbers. Then you can write nice clear expressions using them. In Java, you'd have to use relatively clumsy method calls.

HOWEVER, beguiling though this facility first seems in C++, my opinion is that it is THOROUGHLY EVIL. This is because of the wide variety of "clever" abuses of operators that people magic-up. It easily gets so you have no idea what the code you're reading means! Anyone who has used CORBA C++ binding knows what I mean. It's totally impenetrable and incredible error-prone.

Java may make your code a bit more verbose, but at least you can trust that the code really does what it looks like it does. If you see a + or a [ in Java, you know exactly what it does, without consulting documentation.

Trust me, you don't really want operator overloading!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adrienne Peck:
the java.lang.* import has been added to the class.


You never need to import anything in java.lang package. They are all imported by default. Note that this does not include sub-packages of java.lang, like java.lang.reflect.

you should also avoid using wildcard imports. Always import classes explicitly. It may seem long-winded, but it will avoid ambiguities.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Therefore, the original poster's question is not a stupid one.


Well, the bit about multiplying a number by "100" (not 100) is a bit dim! Perhaps the original poster has previously worked in JavaScript or similar scripting language, which would automatically swap between strings and numbers, to "help" you (in my experience, half its "helpful" type conversions are exactly what I didn't want, but YMMV).
 
Adrienne Peck
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another arithmetic operation question. I have coded the following logic:

if ((Integer.parseInt(bpt.Current_Billing_Period_2))==20) {
Integer.parseInt((bpt.Current_Billing_Period_2).valueOf(1));
if ((Integer.parseInt(bpt.Current_Billing_Month_2))==12) {
Integer.parseInt((bpt.Current_Billing_Month_2).valueOf(1));
(Integer.parseInt((bpt.Current_Billing_Year_2)))++;
}else
Integer.parseInt((bpt.Current_Billing_Month_2).valueOf(1));
}else
Integer.parseInt((bpt.Current_Billing_Period_2).valueOf(1))+=1;


The object bpt is a db object. The fields are being updated based on the content. The code that I am having problems with is when I want to increment the value ' (Integer.parseInt((bpt.Current_Billing_Year_2)))++;'. I am getting the message 'invalid argument to operation ++/-- '. Not sure what this means...any advice?
 
Adrienne Peck
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, what is the difference between the following:

Integer.parseInt((bpt.Current_Billing_Period_2).valueOf(1)) +=1
and
Integer.parseInt((bpt.Current_Billing_Period_2).valueOf(1))++
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrienne, welcome to the Ranch.
Go back to your bit about parsing the String, where you were almost correct.
I think the problem you had was to do with putting inverted commas round the 100, and that your () didn't pair up correctly. It is sometimes difficult to see () and {} and see whether they are paired correctly.
This ought to work:-

Integer.parseInt((bpt.Current_Billing_Period_2).valueOf(1)) +=1
and
Integer.parseInt((bpt.Current_Billing_Period_2).valueOf(1))++
I don't think (just eyeballing them) that either of them will work correctly.
You can increase the value of a number like this:-If "number" was 5 before, it is now 6.
Another way to do it isIf number1 started off life as 5, it is now 6, but number2 is 5. That is because the postfixed ++ operator (the unary postincrement) increases its operand by 1, but during the present statement, it returns its old value.

The preincrement operator has a different effect (++n).

Try this sort of thing:-Then repeat the whole thing with n-- and --n.
You will have to get used to the behaviour of these prefixes and postfixes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!