This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to check entered value is of which primitive type? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to check entered value is of which primitive type?" Watch "How to check entered value is of which primitive type?" New topic
Author

How to check entered value is of which primitive type?

Naresh BabuP
Greenhorn

Joined: Apr 05, 2009
Posts: 5
Hello,

How to check entered value is of which primitive type? Say, entered value is from command prompt.

regards,
Naresh
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

Before thinking of anything, which primitive would you say "0" is ?


[My Blog]
All roads lead to JavaRanch
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 676

In that case you can make Regex pattern and check if contains only number form [0-9] means int. If contains Alphabet from [A-Za-z0-9] and other special character then String. If input contains [0-9] with (DOT .) then float. you can set it according to your requirement.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Important question: what is "0"? Is it:
a) a byte
b) a short
c) a char
d) an int
e) a long
f) a float
g) a double
h) all of the above

These kinds of decisions are very important; do you always take int if possible, ignoring byte and short? Is char only every 1-character String that is not a digit?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Use Scanner:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Somebody else had a similar question recently: here.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to check entered value is of which primitive type?
 
Similar Threads
Comparing value to it's object/primitive type
arrays
How to check whether the entered value is what type
Clean way of checking if object type is primitive or String
Downcasting