何判断输入的的是String还是int的?

taocheng77 2008-04-14 11:37:57
java中如何判断输入的的是String类型还是int类型的?
谢谢..
...全文
616 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuyou001 2008-04-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hmsuccess 的回复:]
Java codepublicbooleanisNumeric(String str)
{
Pattern pattern=Pattern.compile("[0-9]*");
Matcher isNum=pattern.matcher(str);if(!isNum.matches() )
{returnfalse;
}returntrue;
}


[/Quote]
Shine_Panda 2008-04-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 anqini 的回复:]
Java code
public boolean isNumeric(String str)
{
if(str.matches("[1-9][0-9]*")){
// 匹配ok
return true;
}
return false;// 匹配error
}
[/Quote]
来晚了
顶一个..........
hl8189 2008-04-20
  • 打赏
  • 举报
回复
可以用String.match("\\d+").很容易校验的。
wenzheng38 2008-04-20
  • 打赏
  • 举报
回复
引用五楼的,如果用Integer.parseInt(String str)报错就是字符型。否则就是数字型
try
{
Integer.parseInt("your input");或者Integer.valueof("");
//数字
}
catch(Exception e)
{
//字符
}
qusic 2008-04-20
  • 打赏
  • 举报
回复
汗~不过一个问题而已嘛,干嘛这么多重复的 回答
Crazying111 2008-04-20
  • 打赏
  • 举报
回复
用assci 码
云上飞翔 2008-04-20
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 fuyou001 的回复:]
引用 4 楼 hmsuccess 的回复:
Java codepublicbooleanisNumeric(String str)
{
Pattern pattern=Pattern.compile("[0-9]*");
Matcher isNum=pattern.matcher(str);if(!isNum.matches() )
{returnfalse;
}returntrue;
}
[/Quote]
答:我也来凑个热闹:请问:"0x1a2b"这个是不是能转成 JAVA的int类型的。你们上边哪一个做到了?
薛定谔之死猫 2008-04-19
  • 打赏
  • 举报
回复
将输入以字符串的形式读入,再使用正规表达式的方式测试是哪种数据类型
junny9985 2008-04-19
  • 打赏
  • 举报
回复
要疯了你 你就不会用instanceof嘛?

Stirng name = "Mary";

boolean flag = name instanceofv String;
flag =true;
ROBINAPOLLO 2008-04-15
  • 打赏
  • 举报
回复
输入的一定是STRING类型...


只能判断输入的字符串是否能转换成数字......
这个应该就很简单了吧
aipb2008 2008-04-15
  • 打赏
  • 举报
回复
你希望是什么,那就用什么类型来接受他,不可能连你自己在输入都不知道输入的什么数据吧?

Scanner类中

//String
next();
nextLine();
//其他基本类型
nextInt();
hmsuccess 2008-04-15
  • 打赏
  • 举报
回复
或者

import java.io.*;
public class Numtest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean b = true;
try {
while (b) {
System.out.print("请输入一个有效的整数:");
String s = (String) br.readLine();
for (int j = 0; j < s.length(); j++) {
if (!(s.charAt(j) >= 48 && s.charAt(j) <= 57)) {
System.out.println("您输入的不是纯数字!请重新输入.");
b = true;
break;
}
else{
b = false;
}
}
if (!b) {
int i = Integer.parseInt(s);
System.out.println("您输入的是数字:" + i);
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

}

luofamous 2008-04-15
  • 打赏
  • 举报
回复

try
{
Integer.parseInt("your input");或者Integer.valueof("");
//数字
}
catch(Exception e)
{
//字符
}
hmsuccess 2008-04-15
  • 打赏
  • 举报
回复

public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )
{
return false;
}
return true;
}



wensheng_zh2007 2008-04-15
  • 打赏
  • 举报
回复
问题模棱两可
wangunix 2008-04-15
  • 打赏
  • 举报
回复
public static boolean isNumeric(String str){

Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches()){
return false;
}
return true;

}
kunmingkunlun 2008-04-15
  • 打赏
  • 举报
回复
以前写过输入一个正数(包括小数)
public static boolean isIncludePoint(String str) {
if (str.startsWith(".") || str.endsWith(".")) {
return false;
}
int n = 0;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).equals(".")) {
n++;
}
}
if (n > 1) {
return false;
}
return true;
}

// 判断一个正数字是否正确,包括小数
public static boolean IsNumber(String str) {
String s = ".0123456789";
if (isIncludePoint(str)) {
for (int i = 0; i < str.length(); i++) {
if (s.indexOf(str.substring(i, i + 1)) == -1) {
return false;
}
}
} else {
return false;
}

return true;
}
xql80329 2008-04-15
  • 打赏
  • 举报
回复
上面都写的差不多了. 结贴 给分吧
J_Factory 2008-04-15
  • 打赏
  • 举报
回复
还是正则好
kevinchj 2008-04-15
  • 打赏
  • 举报
回复
4 5楼 均可取!
very good!
楼主可以结贴了……
加载更多回复(4)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧