Package xinhuiji_day07;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. Text. simpledateformat;
Import java. util. date;
Public class inputdata {
Private bufferedreader Buf = NULL;
Public inputdata (){
This. Buf = new bufferedreader (New inputstreamreader (system. In ));
}
// Obtain keyboard input
Public String getstring (string info ){
String temp = NULL;
System. Out. Print (Info );
Try {
Temp = This. Buf. Readline ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return temp;
}
// Convert to int
Public int getint (string info, string ERR ){
Int temp = 0;
String STR = NULL;
Boolean flag = true;
While (FLAG ){
STR = This. getstring (Info );
If (Str. Matches ("^ \ D + $ ")){
Temp = integer. parseint (STR );
Flag = false; // here the flag is used to jump out of the loop
} Else {
System. Out. println ("Err ");
}
}
Return temp;
}
// Convert to float
Public float getfloat (string info, string ERR ){
Float temp = 0;
String STR = NULL;
Boolean flag = true;
While (FLAG ){
STR = This. getstring (Info );
If (Str. Matches ("^ \ D + .? \ D + $ ")){
Temp = float. parsefloat (STR );
Flag = false; // here the flag is used to jump out of the loop
} Else {
System. Out. println ("Err ");
}
}
Return temp;
}
// Convert to date
Public date getdate (string info, string ERR ){
Date temp = NULL;
String STR = NULL;
Boolean flag = true;
While (FLAG ){
STR = This. getstring (Info );
If (Str. Matches ("^ \ D {4}-\ D {2}-\ D {2} $ ")){
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
Try {
Temp = SDF. parse (STR );
} Catch (exception e ){
// Todo: handle exception
}
Flag = false; // here the flag is used to jump out of the loop
} Else {
System. Out. println ("Err ");
}
}
Return temp;
}
}