Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check entered value is of which primitive type?

 
Naresh BabuP
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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

regards,
Naresh
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before thinking of anything, which primitive would you say "0" is ?
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Scanner:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49783
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else had a similar question recently: here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic