Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Int to String

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quick question....

is it possible for me in any way to read in a natural integer consisting of say 2 or more digits and then "cast" this into a string ? (i know this sounds stupid but i cant find better words with which to express my thoughts )

or

better still: convert it into a string or int array (such that each digit counts as an array component)....

...so i can index each digit....and apply methods such as length(); etc etc....

I'd appreciate a helpin hand.

thx

W.O.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much

i've decided to use String.valueOf();

PS: is it also possible to make use of the toString(); method in this context ?
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String myString = ""+myNumber;
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you could do this:



...or if you are using Tiger (JDK 1.5), you can take adventage of autoboxing. Like this:


[ May 06, 2006: Message edited by: Edwin Dalorzo ]
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Robert Hill: Thanx very simple and uncomplicated...., what exactly is it that you did there?

@Edwin Dalarzo: when I try to use Autoboxing - which i had already attempted befor posting here- (and i use Java 5 as well)- i get this error message:


C:\XXX\XXX.java:37: int cannot be dereferenced
natZahl.toString();


what does this mean?

thanx a bunch!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call a method on a primitive.

If you want the String equivalent of an int, you can use the static method String.valueOf
 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Keith,...

one more question though:

how do i express the following as an "If" statement in Java code ?:

if the digits entered aren't integers, then ....System.out.println("only integers pls!");
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanx very simple and uncomplicated...., what exactly is it that you did there?


The + operator is overloaded for String, hence you are concatenating String objects, the + operator converts the other operand to an String too.

You have to be very careful when you use that technique. Look at this examples:



Depending on the order of the operators you may receive different results.

As for the digits validation, well, I guess you mean checking that a String just contain valid digits.

I will assume you do not mean the same String we just converted from an int, because it would not make sense.

If you have a String and you want to check that it just have valid digits you could use the Character.isDigit() method or Integer.parse() method and check for the NumberFormatException.
[ May 06, 2006: Message edited by: Edwin Dalorzo ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a String and you want to check that it just have valid digits you could use the Character.isDigit() method or Integer.parse() method and check for the NumberFormatException.


Or you could check it against a regular expression:

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a String and you want to check that it just have valid digits you could use the Character.isDigit() method or Integer.parse() method and check for the NumberFormatException.


is this method similar to : parseInt(); ?
if not: in what class do i find "parse();" ?

thank you all so much,...very grateful!
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Integer.toString instead of String.valueOf.
In fact, most implementations (at least the IBM one) calls Integer.toString from String.valueOf(int).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic