identify data type

I repeat this post in this section, because i think i choose the wrong section before. Sorry....

How can i identify the data type of a variable that is returned by the getParameter method?

suppose there is an int myint=5;

that is submited from an html form, or sent by another page.

i get the variable with request.getParameter("myint")

and ofcourse put it in a String.

How can i check what it was before?

[448 byte] By [akisa] at [2007-10-3 5:25:30]
# 1
> How can i check what it was before?You can only take a guess. There's not way that you can tell what it was before you made it a string.
kajbja at 2007-7-14 23:32:42 > top of Java-index,Java Essentials,New To Java...
# 2

You can't know what it was before. You get a String and nothing more.

You can check wether a value can be interpreted as a number and stored in a String:

int i = Integer.parseInt(theString);

If it is can not be interpreted as an integer, you'll get a NumberFormatException that you can catch.

JoachimSauera at 2007-7-14 23:32:42 > top of Java-index,Java Essentials,New To Java...
# 3
i' ve been searching for a few hours....thanks anyway.it seems i 'll be using the best way of all (!): if(str.equals("1") && ........)
akisa at 2007-7-14 23:32:42 > top of Java-index,Java Essentials,New To Java...
# 4

So you have an infinite number of str.equals(<num>) in that if statement?

USe the poster aboves solution of parseInt, like this:

try{

int myInt = Integer.parseInt(str);

}

catch (NumberFormatException nfe)

{

System.out.println("This is not a number");

}

SomeoneElsea at 2007-7-14 23:32:42 > top of Java-index,Java Essentials,New To Java...
# 5

You can use regular expression to check the string pattern. Use match method of String object to check this.

if (str.match("[0-9]+"){

// str contains only numeric character.

}

Vijay.Kumara at 2007-7-14 23:32:42 > top of Java-index,Java Essentials,New To Java...
# 6

> You can use regular expression to check the string

> pattern. Use match method of String object to check

> this.

>

> [code]

> if (str.match("[0-9]+"){

>// str contains only numeric character.

> /code]

But it does still not solve them problem. The data type that was sent could have been a string containing only digits.

kajbja at 2007-7-14 23:32:42 > top of Java-index,Java Essentials,New To Java...