Originally posted by Candy Bortniker:
I have a int total and a long text. I want to subtract text from total but I get a type mismatch error. I have tried to cast the long as a int but that gave me an error also. What do I need to do to fix this?
What exactly is long text? I know what an int is. It is a 32 bit signed quantity that is defined as part of the Java(tm) language.
Please show us the declaration of the variable that is of type long text. Is it a long, a String, or perhaps a class that someone has defined?
Don't be afraid to post relevant code examples to help explain your problems. Code can really help to explain problems at times.
If you do post a code example, please be sure to surround it with the [code] and [/code] UBB tags in order to help preserve formatting and readability.
When you subtract "text" from "total", the result has type long and must be cast to int. I assume that's what you're trying to do.
A couple of points:
1. If you're going to do some totalizing, use the widest type of interest to do it so you don't potentially lose information, so make "total" a long.
2. Selecting a name "text" for a field of type long is a bad idea as it's very misleading.
Originally posted by Lalit K Kumar:
try this one out.
To convert long to int :-
To convert int to long :-
[ February 26, 2004: Message edited by: Lalit K Kumar ]
This seems like a very convoluted way to convert from one numeric type to another. you should use the cast operators instead:
To convert from int to long:
text = text + (long)total;
To convert from long to int:
total = total + (int)text;
Another solution is to just declare both variables as the same type.