您的位置:首页 > 编程语言 > Java开发

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

2011-09-27 16:39 2141 查看
转自 : /article/4487158.html

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++);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐