• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converts integer into string

 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i convert an integer into a string? i want to be able to get the length of the number, such as 34567 being 5 characters long. but, when i try this i get a compiler error saying that int was detected. here is my code:

[This message has been edited by Thomas Whalen (edited November 25, 2001).]
 
Ben Carlson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a quick fix... I haven't found (then again I haven't looked) another way of doing this...
String str = "" + integer;
To convert a string to an integer, use this:
int i = Integer.parseInt(String);
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, I got it. here is the code to get the length of an integer:


 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, convention dictates that Class names begin with upper-case, and variable/method names begin with lower case.
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, forgot about that ...what about variable names containing more than one word? which of the below styles is best for naming multi-word varibles?
variableFOO
variable_FOO
variable_foo
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Whalen:
What about variable names containing more than one word? which of the below styles is best for naming multi-word varibles?

variableFOO
variable_FOO
variable_foo


None of the above. Try

variableFoo
moreThanOneWordVariable

Only if you're using an acronym
theOneAndOnlyHTTPMethod()
myURLFinder

 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the original question - to convert any variable (either primitive or object) to a String, use String.valueOf: - Peter
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you peter. much easier than using Integer.toString() method, THEN getting length of string.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic