This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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 )
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....
If you want the String equivalent of an int, you can use the static method String.valueOf
Joined: Dec 05, 2005
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!");
Joined: Dec 31, 2004
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 ]