确定String是否是Java中的整数 [英] Determine if a String is an Integer in Java

查看:152
本文介绍了确定String是否是Java中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定字符串数组中的特定项是否为整数。

I'm trying to determine if a particular item in an Array of strings is an integer or not.

.split( ) String 表单中的中缀表达式,然后尝试将结果数组拆分为两个数组;一个用于整数,一个用于操作符,一个用于丢弃括号和其他杂项。实现这一目标的最佳方法是什么?

I am .split(" ")'ing an infix expression in String form, and then trying to split the resultant array into two arrays; one for integers, one for operators, whilst discarding parentheses, and other miscellaneous items. What would be the best way to accomplish this?

我以为我可以找到一个 Integer.isInteger(String arg)方法或其他东西,但没有好运。

I thought I might be able to find a Integer.isInteger(String arg) method or something, but no such luck.

推荐答案

最天真的方法是迭代字符串并确保所有元素都是有效数字。给定基数。这几乎和它可能获得的效率一样高,因为你必须至少查看一次每个元素。我想我们可以根据基数对它进行微观优化,但是对于所有意图和目的来说,这都是你期望获得的。

The most naive way would be to iterate over the String and make sure all the elements are valid digits for the given radix. This is about as efficient as it could possibly get, since you must look at each element at least once. I suppose we could micro-optimize it based on the radix, but for all intents and purposes this is as good as you can expect to get.

public static boolean isInteger(String s) {
    return isInteger(s,10);
}

public static boolean isInteger(String s, int radix) {
    if(s.isEmpty()) return false;
    for(int i = 0; i < s.length(); i++) {
        if(i == 0 && s.charAt(i) == '-') {
            if(s.length() == 1) return false;
            else continue;
        }
        if(Character.digit(s.charAt(i),radix) < 0) return false;
    }
    return true;
}

或者,您可以依赖Java库来实现此目的。它不是基于异常的,并且可以捕获您能想到的每个错误条件。它会贵一点(你必须创建一个Scanner对象,它在一个你不想做的非常紧凑的循环中。但它通常不应该太昂贵,所以对于日常它应该非常可靠。

Alternatively, you can rely on the Java library to have this. It's not exception based, and will catch just about every error condition you can think of. It will be a little more expensive (you have to create a Scanner object, which in a critically-tight loop you don't want to do. But it generally shouldn't be too much more expensive, so for day-to-day operations it should be pretty reliable.

public static boolean isInteger(String s, int radix) {
    Scanner sc = new Scanner(s.trim());
    if(!sc.hasNextInt(radix)) return false;
    // we know it starts with a valid int, now make sure
    // there's nothing left!
    sc.nextInt(radix);
    return !sc.hasNext();
}

如果最佳做法对您无关紧要,或者您想要对进行代码审查的人进行欺骗,请尝试以下尺寸:

If best practices don't matter to you, or you want to troll the guy who does your code reviews, try this on for size:

public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    } catch(NullPointerException e) {
        return false;
    }
    // only got here if we didn't return false
    return true;
}

这篇关于确定String是否是Java中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆