Custom Data input class

Source: Internet
Author: User

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;
}



}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.