JAVA基础之IO键盘输入

/*=====================
 * 知识点:
 * 
 * 键盘输入格式:
 * 日期的转换,以及包装类的使用
 * 类的设计思路
 * 
 *====================*/
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) ;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值