• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

converting int to long

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is longtext. The variable is called text, the type is long.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic