Problem premise:
When entering the price, often is not clear is an integer or a decimal, want to finally be when the user input is an integer automatically becomes an integer, the input decimal becomes a decimal, and the price is more than 0 of the number is required, so it is reasonable, the following is my attempt,
using regular Expressions
1. At the beginning, I wrote a method that can be judged as an integer.
/**
* Determine if the integer is greater than 0
*
* @param str
* @return
*/
public static double Isinteger (String str) {
Double result;
Pattern pattern = Pattern.compile ("^1[-\\+]?[ \\d]*$ ");
Pattern pattern = pattern.compile ("^[1-9][\\d]*$");
if (!pattern.matcher (str). Matches ()) {
SYSTEM.OUT.PRINTLN ("You entered is not a number or illegal, please re-enter a number greater than 0!") ");
Scanner sc = new Scanner (system.in);
result = Isinteger (Sc.next ());
} else {
result = Double.parsedouble (str);
}
return result;
}
OK, able to cope with a variety of non-and hair input
2. When you add a decimal that can be judged,
public static double Isdata (String str) {
Double result = 0;
Scanner SC =new Scanner (system.in);
Pattern pattern = pattern.compile ("^[1-9][\\d]*$");
Pattern pattern2 = Pattern.compile ("^[0-9]+.*[0-9]*");
if (".". Equals (str)) {
SYSTEM.OUT.PRINTLN ("You entered is not a number or illegal, please re-enter a number greater than 0!") ");
}else
if (!pattern.matcher (str). Matches ()) {
if (!pattern2.matcher (str). Matches ()) {
SYSTEM.OUT.PRINTLN ("You entered is not a number or illegal, please re-enter a number greater than 0!") ");
result = Isdata (Sc.next ());
}else{
result = Double.parsedouble (str);
}
}else{
result = Integer.parseint (str);
}
return result;
}
It looks like it's gone, but it's still not working.
Especially when you enter 23-2 or 22\22, you hang up.
3. Last Word online swing one
Using a regular expression to determine whether a string is a number, this way to judge the surface comparison is full//can be judged positive and negative, integer decimal boolean strresult = Str.matches ("-?[ 0-9]+.*[0-9]* "), if (strresult = = True) {System.out.println (" is number! "),} else {System.out.println (" is not number! ");}
for this question, I finally chose the
Java's self-bringing approach
public static void Main (string[] args) {
String a= "null";
Isdilg ();
}
/**
* Determine whether to enter an integer or decimal number greater than 0
*/
public static void Isdilg () {
int i=0;
Double d = null;
System.out.println ("\ n sdf");
Scanner SC =new Scanner (system.in);
do{
int a=-2;
if (!sc.hasnextint ()) {
if (!sc.hasnextdouble ()) {
System.out.println ("------------");
SYSTEM.OUT.PRINTLN ("You entered the illegal, please re-enter!") ");
Isdilg ();
}else{
D=sc.nextdouble ();
if (d>0) {
System.out.println (d);
}else{
SYSTEM.OUT.PRINTLN ("You entered the illegal, please re-enter!") ");
Isdilg ();
}
}
}else{
I=sc.nextint ();
if (i>0) {
System.out.println (i);
}else{
SYSTEM.OUT.PRINTLN ("You entered the illegal, please re-enter!") ");
Isdilg ();
}
}
}while (TRUE);
}
Yes, OK, no killing, or someone else Java method Good, this is really not a regular expression is not very useful, and consider not the whole classmate,
Or with the Java API simple!
About entering a price question, which is a number greater than 0