| Author |
converting int to long
|
Candy Bortniker
Ranch Hand
Joined: Mar 17, 2003
Posts: 123
|
|
|
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?
|
 |
Lalit K Kumar
Ranch Hand
Joined: Jan 29, 2004
Posts: 32
|
|
hi, try this one out. To convert long to int :- total =total+Integer.parseInt(String.valueOf(text)); To convert int to long :- text =text+Long.parseLong(String.valueOf(total)); regards, lalit [ February 26, 2004: Message edited by: Lalit K Kumar ]
|
 |
Howard Kushner
author
Ranch Hand
Joined: Sep 19, 2003
Posts: 361
|
|
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?
Candy, 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? Regards,
|
Howard Kushner<br />IBM Certified Enterprise Developer - WebSphere Studio Application Developer V5.0<br />IBM Certified Advanced System Administrator - WebSphere Application Server V5.0<br />IBM Certified Solution Developer - Web Services with WebSphere Studio V5.1<br /><a href="http://www.amazon.com/exec/obidos/tg/detail/-/1931182108/" target="_blank" rel="nofollow">Developing J2EE Applications with WebSphere Studio</a> my Certification Study Guide for IBM Test 287
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Candy, 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.
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Candy Bortniker
Ranch Hand
Joined: Mar 17, 2003
Posts: 123
|
|
|
It is longtext. The variable is called text, the type is long.
|
 |
Ken Krebs
Ranch Hand
Joined: Nov 27, 2002
Posts: 451
|
|
Candy, 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.
|
kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Originally posted by Lalit K Kumar: hi, try this one out. To convert long to int :- total =total+Integer.parseInt(String.valueOf(text)); To convert int to long :- text =text+Long.parseLong(String.valueOf(total)); regards, lalit [ 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.
|
Java API Documentation
The Java Tutorial
|
 |
 |
|
|
subject: converting int to long
|
|
|