@V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。

来源:互联网 发布:混凝土抗风柱计算软件 编辑:程序博客网 时间:2024/05/17 21:56

转自  : http://jayxigua.iteye.com/blog/691025 

 

package jay_x_20100612;import java.io.*;import java.util.regex.Pattern;public class jay_x_控制台输入各类型类实现 { public static int v = 9; public static void main(String[] args) {  InputStreamReader stdin = new InputStreamReader(System.in);  BufferedReader bufin = new BufferedReader(stdin);  try {   int i2 = Integer.parseInt(bufin.readLine());   System.out.print(i2);   // 判断输入的内容里面是否有数字   String s2 = bufin.readLine();   System.out.print(s2);   // 方法1:转换成字符数组,然后一个一个的比较是否含有数字   char ch3[] = s2.toCharArray();   Character ch6 = null;   // char ch4[]={'1','2'};   // char ch5=ch4[1];   for (int m = 0; m < ch3.length; m++) {    System.out.print(ch6.isDigit(ch3[m]));   }   // static boolean isDigit(char ch)   // 确定指定字符是否为数字。   // static boolean isDigit(int codePoint)   // 确定指定字符(Unicode 代码点)是否为数字。   // 方法2:正则表达式,比较整个字符串中是否符合数字格式   String s3 = bufin.readLine();   Pattern p = Pattern.compile("[0-9]*");   System.out.print(p.matcher(s3).matches());   // new a().a;  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  System.out.print(input.readString());  System.out.print(input.readChar());  System.out.print(input.readInt());  System.out.print(input.readFloat());  System.out.print(input.readDouble());   System.out.print(v);  new a().u();  System.out.print(v); }}class input { static InputStreamReader in; static BufferedReader reader; static {  in = new InputStreamReader(System.in);  reader = new BufferedReader(in); } static String readString() {  String s = null;  try {   s = reader.readLine();  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   System.exit(0);  }  return s; } static char readChar() {  String s = null;  char ch = 'a';  try {   s = reader.readLine();   ch = s.charAt(0);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   System.exit(0);  }  return ch; } static int readInt() {  String s;  int i = 0;  try {   s = reader.readLine();   i = Integer.parseInt(s);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   System.exit(0);  }  return i; } static float readFloat() {  String s;  float f = 0.0f;  try {   s = reader.readLine();   f = Float.parseFloat(s);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   System.exit(0);  }  return f; } static double readDouble() {  String s;  double d = 0.0;  try {   s = reader.readLine();   d = Double.parseDouble(s);  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   System.exit(0);  }  return d; }}class a { void u() {  System.out.print(new jay_x_控制台输入各类型类实现().v++); }}


 

原创粉丝点击