/*=====================
* 知识点:
*
* 键盘输入格式:
* 日期的转换,以及包装类的使用
* 类的设计思路
*
*====================*/
package demoIO012;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class InputMyData{
private BufferedReader buf=null;
public InputMyData(){
this.buf=new BufferedReader(new InputStreamReader(System.in));
}
public String getString(String info) throws IOException{
String temp=null;
System.out.println("提示信息:"+info);
temp=this.buf.readLine();
return temp;
}
public int getInt(String info,String err) throws IOException{
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;
}else{
System.out.println("错误信息:"+err);
}
}
return temp;
}
public float getFloat(String info,String err) throws IOException{
float temp=0;
String str=null;
boolean flag=true;
while(flag){
str=this.getString(info);//获取输入的字符
if(str.matches("^\\d+$")){//正则表达式 判断是否由数字组成
temp=Integer.parseInt(str);
flag=false;
}else{
System.out.println("错误信息:"+err);
}
}
return temp;
}
public Date getData(String info,String err) throws IOException{
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 (ParseException e) {
e.printStackTrace();
}
flag=false;
}else{
System.out.println("错误信息:"+err);
}
}
return temp;
}
//..............
}
public class DemoIO0001 {
public static void main(String args[]) throws IOException{
//demo1();
// demo2();
//demo3();
demo4();
}
public static void demo4() throws IOException{
InputMyData mydata=new InputMyData();
Date d=mydata.getData("请输入日期", "日期的格式必须为YYYY-dd-xx");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
System.out.println("你输入的日期是:"+sdf.format(d));
}
public static void demo3() throws IOException{
InputMyData mydata=new InputMyData();
int i=mydata.getInt("输入数字:", "出错提示:");
System.out.println("你的输入:"+i);
}
public static void demo2() throws IOException{
int i=0;
int j=0;
InputMyData mydata=new InputMyData();
i =mydata.getInt("请输入第一个信息:","第一个信息必须是数字,如果不是必须重新输入!");
j=mydata.getInt("请输入第二个信息:","第一个信息必须是数字,如果不是必须重新输入!");
System.out.println("加法:"+(i+j));
}
public static void demo1() throws IOException{
int i=0;
int j=0;
BufferedReader buf=null;
buf=new BufferedReader(new InputStreamReader(System.in));
String str=null;
System.out.print("输入第一个数:");
str=buf.readLine();
i=Integer.parseInt(str);
System.out.print("请输入第二个数:");
str=buf.readLine();
j=Integer.parseInt(str);
System.out.println("结果:"+(i+j));
}
}
/**
* 本程序可用吗?这问题要时常的问自己。
* 现在如果用户输入的如果不是数字呢?本程序自输入整数。
* 代码重复,一使用输入数据,则肯定使用BufferedStreamReader ,代码重复;
* 原因:
* 代码设计不合理:
* 输入的可能是:整数,小数,日期,字符串;因此,我们最好设计出一个专门提供输入的类
* 来完成输入数据的功能,而且在此类中还可以对输入的数据进行验证
*/
Scanner类键盘输入,修改分隔符
修改
import java.util.Scanner;
public class demoIO002 {
public static void main(String args[]){
Scanner scan = new Scanner(System.in) ; // 从键盘接收数据
System.out.print("输入数据:") ;
scan.useDelimiter("\n") ;
String str = scan.next() ; // 接收数据
System.out.println("输入的数据为:" + str) ;
}
}