It realizes the type judgment of scanner content, type coercion.
Content to scan: String,double,float,integer
1 Public classGetData {2 Private StaticScanner Scanner;3 Static {4Scanner =NewScanner (system.in);5 }6 7 PrivateGetData () {8 }9 Ten /** One * Return String type A * - * @return - */ the Public StaticString getString () { - returnscanner.nextline (); - } - + /** - * Return float type, possibly null + * A * @return at */ - Public StaticFloat getfloat () { - -String string =scanner.nextline (); - Float F; - Try { inf =float.parsefloat (string); - returnF; to}Catch(NumberFormatException e) { + //TODO auto-generated Catch block - //e.printstacktrace (); the return NULL; * } $ }Panax Notoginseng - /** the * Gets the integer type value. + * A * @return the */ + Public StaticInteger Getinteger () { -String string =scanner.nextline (); $ integer integer; $ Try { -Integer =integer.parseint (string); - returninteger; the}Catch(NumberFormatException e) { - //e.printstacktrace ();Wuyi return NULL; the } - Wu } - About /** $ * Gets the double type value. - * - * @return - */ A Public StaticDouble getdouble () { +String string =scanner.nextline (); the Double integer; - Try { $Integer =double.parsedouble (string); the returninteger; the}Catch(NumberFormatException e) { the //e.printstacktrace (); the return NULL; - } in } the the /** About * Close Scanner the */ the Public Static voidClosescanner () { the scanner.close (); + } -}
Scanner Scanner Tool class