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
Joined: Dec 31, 2004
Posts: 961
posted
0
How about this?
Wolfgang Obi
Ranch Hand
Joined: Dec 05, 2005
Posts: 134
posted
0
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
Joined: Feb 24, 2006
Posts: 94
posted
0
String myString = ""+myNumber;
Edwin Dalorzo
Ranch Hand
Joined: Dec 31, 2004
Posts: 961
posted
0
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
Joined: Dec 05, 2005
Posts: 134
posted
0
@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
Joined: Feb 07, 2005
Posts: 2341
posted
0
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
Joined: Dec 05, 2005
Posts: 134
posted
0
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
Joined: Dec 31, 2004
Posts: 961
posted
0
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
Joined: Jan 17, 2006
Posts: 1295
posted
0
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:
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Wolfgang Obi
Ranch Hand
Joined: Dec 05, 2005
Posts: 134
posted
0
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
Joined: Sep 24, 2003
Posts: 1608
posted
0
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).