Home > Back-end >  Determine the input data type
Determine the input data type

Time:10-06

If you use the scanner in the next () should be how to determine the input number is plastic or floating point?

CodePudding user response:

1, transformation, 2, and check whether there is a decimal point

CodePudding user response:

reference 1st floor with ah response:
1, transformation, 2, and check whether there is a decimal point

Transformation is to point to what? The second is the string into an array in traverse any decimal point?

CodePudding user response:

import java.util.Scanner;
The class Test {
Public static void main (String [] args) {
System. The out. Println (" input a number ");
Scanner tem=new Scanner (System. In);
String STR=tem. Next ();
Int s=STR. IndexOf (". ");
If (s> 0 {
System. The out. Println (" is a floating point number ");
}
Try {
Int ss=Integer. ParseInt (STR);
System. The out. Println (" is an integer ");
} the catch (a NumberFormatException e) {
System. The out. Println (" is not the integer ");
}

}
}

CodePudding user response:

import java.util.Scanner;
Import the Java. Util. Regex. The Pattern;
Class a {
Public static void main (String [] args) {
System. The out. Println (" input a number ");
Scanner item=new Scanner (System. In);
String STR=item. The next ();
The Pattern running the stra=Pattern.com (" ^ [\ \ +]? [\ \ d] * $");
The Pattern running the STRB=Pattern.com (" ^ [\ \ +]? [. \ \ d] * $");
If (stra. Matcher (STR). Matches ()) {
System. The out. Println (" is an integer ");
} else if (STRB matcher (STR) matches ()) {
System. The out. Println (" is a floating point number ");
} else {
System. The out. Println (" beyond the scope of the number ");
}
}
}

CodePudding user response:

 
Scanner s=new Scanner (System. In);
Sheldon horowitz asNextInt ();
Sheldon horowitz asNextDouble ();
Sheldon horowitz asNext ()//string
  • Related