何判断输入的的是String还是int的? java中如何判断输入的的是String类型还是int类型的?谢谢.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if( !isNum.matches() ) { return false; } return true;} 用 try{Integer.parseInt("your input");或者Integer.valueof("");//数字}catch(Exception e){ //字符} 或者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(); } }} 你希望是什么,那就用什么类型来接受他,不可能连你自己在输入都不知道输入的什么数据吧?Scanner类中//Stringnext();nextLine();//其他基本类型nextInt();… 输入的一定是STRING类型...只能判断输入的字符串是否能转换成数字......这个应该就很简单了吧 以前写过输入一个正数(包括小数)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; } public static boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if( !isNum.matches()){ return false; } return true; } public boolean isNumeric(String str){ if(str.matches("[1-9][0-9]*")){ // 匹配ok return true; } return false;// 匹配error} 4 5楼 均可取!very good!楼主可以结贴了…… 要疯了你 你就不会用instanceof嘛?Stirng name = "Mary";boolean flag = name instanceofv String; flag =true; 引用五楼的,如果用Integer.parseInt(String str)报错就是字符型。否则就是数字型 try { Integer.parseInt("your input");或者Integer.valueof(""); //数字 } catch(Exception e) { //字符 } 可以用String.match("\\d+").很容易校验的。 答:我也来凑个热闹:请问:"0x1a2b"这个是不是能转成 JAVA的int类型的。你们上边哪一个做到了? swing 后台操作事件长 , 界面如何显示正在等待? 笔记本不能正常放歌了 关于java程序打包成安装程序的问题!!求明白的高人解答~~ 在JTextField时回车触发按钮,如何实现??? 求助,如何得出两个时间之间相差多少个月的集合 如何的出啊???? MS IE浏览器自带的java虚拟机比sun的jre 缺少那些包的支持? java的输入问题 急!急!急!JAVA是否开源 类定义的问题!! jb9的问题!!请大虾多帮忙,不要让我这个初学者失去信心! JTable 设置能选择但双击不能被修改 来一个sun公司的笔试题!看看大家的答案
public boolean isNumeric(String str)
{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )
{
return false;
}
return true;
}
try
{
Integer.parseInt("your input");或者Integer.valueof("");
//数字
}
catch(Exception e)
{
//字符
}
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();
} }}
next();
nextLine();
//其他基本类型
nextInt();
…
只能判断输入的字符串是否能转换成数字......
这个应该就很简单了吧
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;
}
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches()){
return false;
}
return true;
}
public boolean isNumeric(String str)
{
if(str.matches("[1-9][0-9]*")){
// 匹配ok
return true;
}
return false;// 匹配error
}
very good!
楼主可以结贴了……
flag =true;
try
{
Integer.parseInt("your input");或者Integer.valueof("");
//数字
}
catch(Exception e)
{
//字符
}