• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding String, int

 
Edward Durai
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Consider I have
e.g

String str = "Test INPUT";
int it = 44;

How can I check String "str" is a string type
integer "it" is int type.

Please let me know.
Thanking You
Regards,
edward
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you're asking. You know that str is a String, because you have defined it that way.

If you have an Object reference "str", you can check whether it is a String via "str instanceof String", which returns a boolean.

Can you give us some more context about the problem you're trying to solve?
[ August 01, 2007: Message edited by: Ulf Dittmer ]
 
Edward Durai
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider i have one text box,

If I enter any value,

for e.g if i enter 100, i want to see in console, "integer value"
if i enter text, it should display "string value"
if i enter date, it should display "date".

Please let me know
Thanking You.
Regards,
Edward
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you enter text into a text-box, that text is represented as a String, always. You could write code that examines that String and decides whether it makes sense to parse that String into a Date, or an int, or some other type.

See also:
Integer.parseInt()
DateFormat.parse()
[ August 01, 2007: Message edited by: Garrett Rowe ]
 
Ashwin Sharma
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi....
This may help
try{
Integer.parseInt("2 ");
System.out.println("Integer");
}catch(NumberFormatException e)
{
System.out.println("String");
}
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edward Durai:

String str = "Test INPUT";
int it = 44;


What do you give me to solve this question ?

If you give me a java object reference and ask me whether its pointing to String object or a int primitive , then I would say its String as reference to String object cannot contain int primitive.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic