请问,在运行JAVA类时输入参数,怎样判断输入的参数是“数字型”的字符串还是“字母或其他类型”的字符串? 比如在程序中有个方法要接受该参数进行运算,如果输入的是任意数,则可用Integer.parseInt(args[0])转换,如果是“abcd、4t56”等类的字母或其他符号则不接受参数。代码因该怎么写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;class GetInt{ public int getInt() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { String str=br.readLine(); if(str.length()==0) return -1; for(int i=0;i<str.length();i++) { if(str.charAt(i)<'0'||str.charAt(i)>'9') return -1; } return Integer.parseInt(str); } catch(IOException e) { System.out.println("输入有错!"); return -1; } }} class Input{ public static void main(String[] args) { GetInt get=new GetInt(); System.out.println("输入一个整数"); int num=get.getInt(); while(num==-1) { System.out.println("输入有错,请输入一个整数"); num=get.getInt(); } System.out.println(num); } } boolean b = false;try{ int i =Integer.parseInt(args[0]);}catch(Exception e){ b=true;}if(b){ System.out.println("输入有错,请输入一个整数");} 参考 commons-util 里面的StringUtils类 队列的tostring方法 同步方法到底锁定了那个对象? 多线程 JTable如何设置为半透明 在java中输出希腊字母,却发现这个问题,为什么呢? 如何使用新的包? jdk-1_5_0_06下ArrayList<E>的问题 怎么样随机读取一个unicode文件或是,在一个普通的中英文 混合文件中,以空格为界随机读取。。。 关于“this”指针的问题。 又一个关于JAVA的问题,还是基本性质的题目,爱好者们进来研究研究 JAVA在讀數據庫的時候,可否轉到指定的某一行? 变量的定义
import java.io.IOException;
import java.io.InputStreamReader;
class GetInt
{
public int getInt()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
String str=br.readLine();
if(str.length()==0)
return -1;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)<'0'||str.charAt(i)>'9')
return -1;
}
return Integer.parseInt(str);
}
catch(IOException e)
{
System.out.println("输入有错!");
return -1;
}
}
}
class Input
{
public static void main(String[] args)
{
GetInt get=new GetInt();
System.out.println("输入一个整数");
int num=get.getInt();
while(num==-1)
{
System.out.println("输入有错,请输入一个整数");
num=get.getInt();
}
System.out.println(num);
}
}
try{
int i =Integer.parseInt(args[0]);
}
catch(Exception e){
b=true;
}
if(b){
System.out.println("输入有错,请输入一个整数");
}