[java]判断输入字符串类型( 附测试数据)
来源:互联网 发布:网络广告公司 编辑:程序博客网 时间:2024/06/05 16:00
import java.io.*;
/*
* @author: tianrenliang
* @protramme CheckChacter
* @date 2007.10.9
* @version: 1.0
*
*/
public class CheckChacter {
/* check the string whether a binary string */
public boolean CheckBin(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != '0' && str.charAt(i) != '1') {
System.out.println(str + " is not a binary string");
return false;
}
}
System.out.println(str + " is a binary string");
return true;
}
/* check the string whether a decimalist string */
public boolean CheckDec(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
System.out.println(str + " is not a decimalist string");
return false;
}
}
System.out.println(str + " is a decimalist string");
return true;
}
/* check the string whether a hex string */
public boolean CheckHex(String str) {
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) < '0' || str.charAt(i) > '9')
&& (str.charAt(i) < 'a' || str.charAt(i) > 'f')
&& (str.charAt(i) < 'A' || str.charAt(i) > 'F')) {
System.out.println(str + " is not a hex string");
return false;
}
}
System.out.println(str + " is a hex string");
return true;
}
/* check the string whether a character string */
public boolean CheckCha(String str) {
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) < 'a' || str.charAt(i) > 'z')
&& (str.charAt(i) < 'A' || str.charAt(i) > 'Z')) {
System.out.println(str + " is not a character string");
return false;
}
}
System.out.println(str + " is a character string");
return true;
}
/* check the string whether a octal string */
public boolean CheckOct(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '7') {
System.out.println(str + " is not a octal string");
return false;
}
}
System.out.println(str + " is a octal string");
return true;
}
public static void main(String[] args) {
String strA = null;
/*get input frome keyboard*/
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader bufin = new BufferedReader(stdin);
try
{
System.out.print ("please input:");
strA = bufin.readLine();
System.out.println ("your input is:"+strA);
}
catch(IOException E)
{
System.out.println("io exception!");
}
CheckChacter m = new CheckChacter();
m.CheckDec(strA);
m.CheckBin(strA);
m.CheckCha(strA);
m.CheckHex(strA);
m.CheckOct(strA);
}
}
/* these are the test parameter*/
// String dec1="abcd";
// String dec2="123456";
// String dec3="010010";
// String dec4=".20.";
// String dec5="0x11";
// m.CheckDec(dec1);
// m.CheckDec(dec2);
// m.CheckDec(dec3);
// m.CheckDec(dec4);
// m.CheckDec(dec5);
// String bin1="abcd";
// String bin2="123456";
// String bin3="010010";
// String bin4=".20.";
// String bin5="0x11";
// m.CheckBin(bin1);
// m.CheckBin(bin2);
// m.CheckBin(bin3);
// m.CheckBin(bin4);
// m.CheckBin(bin5);
// String hex1 = "abcd";
// String hex2 = "123456";
// String hex3 = "010010";
// String hex4 = ".20.";
// String hex5 = "0x11";
// m.CheckHex(hex1);
// m.CheckHex(hex2);
// m.CheckHex(hex3);
// m.CheckHex(hex4);
// m.CheckHex(hex5);
// String cha1 = "abcd";
// String cha2 = "123456";
// String cha3 = "010010";
// String cha4 = ".20.";
// String cha5 = "0x11";
// m.CheckCha(cha1);
// m.CheckCha(cha2);
// m.CheckCha(cha3);
// m.CheckCha(cha4);
// m.CheckCha(cha5);
// String oct1 = "abcd";
// String oct2 = "123458";
// String oct3 = "010010";
// String oct4 = ".20.";
// String oct5 = "0x11";
// m.CheckOct(oct1);
// m.CheckOct(oct2);
// m.CheckOct(oct3);
// m.CheckOct(oct4);
// m.CheckCha(oct5);
/*
* @author: tianrenliang
* @protramme CheckChacter
* @date 2007.10.9
* @version: 1.0
*
*/
public class CheckChacter {
/* check the string whether a binary string */
public boolean CheckBin(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != '0' && str.charAt(i) != '1') {
System.out.println(str + " is not a binary string");
return false;
}
}
System.out.println(str + " is a binary string");
return true;
}
/* check the string whether a decimalist string */
public boolean CheckDec(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
System.out.println(str + " is not a decimalist string");
return false;
}
}
System.out.println(str + " is a decimalist string");
return true;
}
/* check the string whether a hex string */
public boolean CheckHex(String str) {
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) < '0' || str.charAt(i) > '9')
&& (str.charAt(i) < 'a' || str.charAt(i) > 'f')
&& (str.charAt(i) < 'A' || str.charAt(i) > 'F')) {
System.out.println(str + " is not a hex string");
return false;
}
}
System.out.println(str + " is a hex string");
return true;
}
/* check the string whether a character string */
public boolean CheckCha(String str) {
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) < 'a' || str.charAt(i) > 'z')
&& (str.charAt(i) < 'A' || str.charAt(i) > 'Z')) {
System.out.println(str + " is not a character string");
return false;
}
}
System.out.println(str + " is a character string");
return true;
}
/* check the string whether a octal string */
public boolean CheckOct(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '7') {
System.out.println(str + " is not a octal string");
return false;
}
}
System.out.println(str + " is a octal string");
return true;
}
public static void main(String[] args) {
String strA = null;
/*get input frome keyboard*/
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader bufin = new BufferedReader(stdin);
try
{
System.out.print ("please input:");
strA = bufin.readLine();
System.out.println ("your input is:"+strA);
}
catch(IOException E)
{
System.out.println("io exception!");
}
CheckChacter m = new CheckChacter();
m.CheckDec(strA);
m.CheckBin(strA);
m.CheckCha(strA);
m.CheckHex(strA);
m.CheckOct(strA);
}
}
/* these are the test parameter*/
// String dec1="abcd";
// String dec2="123456";
// String dec3="010010";
// String dec4=".20.";
// String dec5="0x11";
// m.CheckDec(dec1);
// m.CheckDec(dec2);
// m.CheckDec(dec3);
// m.CheckDec(dec4);
// m.CheckDec(dec5);
// String bin1="abcd";
// String bin2="123456";
// String bin3="010010";
// String bin4=".20.";
// String bin5="0x11";
// m.CheckBin(bin1);
// m.CheckBin(bin2);
// m.CheckBin(bin3);
// m.CheckBin(bin4);
// m.CheckBin(bin5);
// String hex1 = "abcd";
// String hex2 = "123456";
// String hex3 = "010010";
// String hex4 = ".20.";
// String hex5 = "0x11";
// m.CheckHex(hex1);
// m.CheckHex(hex2);
// m.CheckHex(hex3);
// m.CheckHex(hex4);
// m.CheckHex(hex5);
// String cha1 = "abcd";
// String cha2 = "123456";
// String cha3 = "010010";
// String cha4 = ".20.";
// String cha5 = "0x11";
// m.CheckCha(cha1);
// m.CheckCha(cha2);
// m.CheckCha(cha3);
// m.CheckCha(cha4);
// m.CheckCha(cha5);
// String oct1 = "abcd";
// String oct2 = "123458";
// String oct3 = "010010";
// String oct4 = ".20.";
// String oct5 = "0x11";
// m.CheckOct(oct1);
// m.CheckOct(oct2);
// m.CheckOct(oct3);
// m.CheckOct(oct4);
// m.CheckCha(oct5);
- [java]判断输入字符串类型( 附测试数据)
- 判断输入的字符串类型
- 判断输入字符串是否是数字类型
- java判断字符串内容类型
- 【java】判断字符串编码类型
- java 判断字符串编码类型
- 判断输入字符串为颜色类型的最优方法
- java 判断字符串什么编码类型
- java 判断字符串什么编码类型
- java 判断字符串什么编码类型
- (转)java 判断字符串什么编码类型
- java判断字符串所属的编码类型
- Java进阶(三十) 判断字符串编码类型
- java判断字符串是否是日期类型
- Java进阶(三十) 判断字符串编码类型
- javascript判断输入类型
- java之判断输入的字符类型个数
- 判断字符串类型 递归
- 自己常用到的DB2命令
- 如何使用.NET配置文件
- VMware安装Tools问题
- VS 2005使用心得(二)
- 常用到的DB2命令
- [java]判断输入字符串类型( 附测试数据)
- Tomcat5配置Mysql JDBC数据库连接池
- 2007软件开发2.0大会讲师介绍
- 整理: MyEclipse 5.5 实战教程(持续更新,转帖)
- JAVA GUI中日期选择控件的实现
- Service Unavailable 错误 解决办法
- 2007软件开发2.0大会海外讲师介绍
- Esmtp邮件发送程序
- winform配置文件的简单使用 ----看看最后一段话。自己迷惑了好长时间。