You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Muthu Kumaran Lekshmanan -X (mlekshma - WIPRO at Cisco)" <ml...@cisco.com> on 2006/01/24 17:51:42 UTC

How to find whether a value passed is string or number



Hi,

I am a rank fresher to velocity, I have been asked to do a few things on
it. I am basically using it to generate string commands based on data
received and send it down to a device.

My problem now is to find if a value passed to me is a charecter string
or a numeric value. This piece of code i wrote for it does not
work.....any pointers on doing it will be highly helpful. Below is code
i wrote which does not work

 
    #set($letterCount=0)
    #set($lng=$stdCommListNo.length() - 1)
    #foreach( $i in [0..$lng] )
        #set($char=$stdCommListNo.charAt($i))
        #if(!$Character.isDigit($char))
            #set($letterCount=$letterCount + 1)
        #end
    #end
    #if($letterCount > 0)
        <Do some printing for string value>
     #else
           <Do some printing for numeric value>
     #end
 
 
Any help in this direction will be highly helpful......please dont mind
if it is too dumb a question....it will motivate me to dig
more......thanks.

Warm Regards,
Muthu Kumaran.L

"Faith is to believe what you do not yet see; the reward for this faith
is to see what you believe."
-Swami Vivekananda


Re: How to find whether a value passed is string or number

Posted by Nathan Bubna <nb...@gmail.com>.
On 1/24/06, Muthu Kumaran Lekshmanan -X (mlekshma - WIPRO at Cisco)
<ml...@cisco.com> wrote:
>
> Hi,
>
> I am a rank fresher to velocity, I have been asked to do a few things on
> it. I am basically using it to generate string commands based on data
> received and send it down to a device.
>
> My problem now is to find if a value passed to me is a charecter string
> or a numeric value. This piece of code i wrote for it does not
> work.....any pointers on doing it will be highly helpful. Below is code
> i wrote which does not work
>
>
>     #set($letterCount=0)
>     #set($lng=$stdCommListNo.length() - 1)
>     #foreach( $i in [0..$lng] )
>         #set($char=$stdCommListNo.charAt($i))
>         #if(!$Character.isDigit($char))

the line above will not work.  Velocity != Java, so you do not
automatically have access to Character.isDigit(char).

>             #set($letterCount=$letterCount + 1)
>         #end
>     #end
>     #if($letterCount > 0)
>         <Do some printing for string value>
>      #else
>            <Do some printing for numeric value>
>      #end
>
>
> Any help in this direction will be highly helpful......please dont mind
> if it is too dumb a question....it will motivate me to dig
> more......thanks.

if i were you, i would move all of that logic into a tool class like:

public class MyCharacterTool {

    public int getLetterCount(String foo) {
        int letterCount = 0;
        for (int i=0; i < foo.length(); i++) {
            if (!Character.isDigit(foo.charAt(i))) {
                letterCount++;
            }
        }
        return letterCount;
    }
}

then you add an instance of MyCharacterTool to your Velocity Context...

context.put("charTool", new MyCharacterTool());

and use it in your template like...

#set( $letterCount = $charTool.getLetterCount($stdCommListNo) )
#if( $letterCount > 0)
  <do some string printing>
#else
  <do some number printing>
#end


> Warm Regards,
> Muthu Kumaran.L
>
> "Faith is to believe what you do not yet see; the reward for this faith
> is to see what you believe."
> -Swami Vivekananda
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org