Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting int to long

 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • 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?
 
Lalit K Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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 ]
 
Howard Kushner
author
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • 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,
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
It is longtext. The variable is called text, the type is long.
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic