先得到对象的类字节码Class对象,通过Class对象再得到类型,而后进行判断是不原始类型方法可能是isPrimitive或数组isArray()。
爪哇是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。
Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。
给你一个封装好的方法,只要把excel中的cell放入就会返回对应的值,里面有类型检测public static String getExcelCellValue(HSSFCell cell) {
String ret = ""
//if (HSSFDateUtil.isCellDateFormatted(cell)) {
//Date date = cell.getDateCellValue()
//ret = "" + date.getTime()
//} else
try {
if (cell == null) {
ret = ""
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
ret = cell.getStringCellValue().trim()
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
ret = "" + cell.getNumericCellValue()
String temp = ret.substring(ret.indexOf(".") + 1, ret.length())
try {
if (Integer.parseInt(temp) == 0) {
ret = ret.substring(0, ret.indexOf("."))
}
} catch (Exception ex) {
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
ret = cell.getCellFormula()
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
ret = "" + cell.getErrorCellValue()
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
ret = "" + cell.getBooleanCellValue()
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
ret = ""
}
} catch (Exception ex) {
ex.printStackTrace()
ret = ""
}
return ret
}
instanceof关键字用来检测变量类型但是你这里全是字符串,所以用正则来实现就容易多了public class Test {
public static void main(String[] args) {
String[] arry = {"12","+","4","h"}
String numReg = "^\\d+$"
String strReg = "^[a-zA-Z]+$"
String opReg = "^(\\+|-|\\*|/)+$"
String[] reg = {numReg,strReg,opReg}
String[] msg = {"Integer","Invalid String","Operator"}
for(int i=0i<arry.lengthi++){
String temp = arry[i]
for(int j=0j<reg.lengthj++){
if(temp.matches(reg[j])){
System.out.println(temp+" "+msg[j])
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)