我在应用程序中加载了一个字符串,它可以从数字变成字母等等。我有一个简单的if语句,看看它是否包含字母或数字,但是,有些东西不太正确。下面是一个片段。

String text = "abc"; 
String number; 

if (text.contains("[a-zA-Z]+") == false && text.length() > 2) {
    number = text; 
}

虽然文本变量包含字母,但条件返回为true。和&&应该eval作为两个条件都必须为真,以便处理number =文本;

==============================

解决方案:

我能够通过使用以下代码来解决这个问题,该代码由对这个问题的评论提供。所有其他帖子也是有效的!

我使用的有效方法来自第一条评论。尽管提供的所有示例代码似乎也是有效的!

String text = "abc"; 
String number; 

if (Pattern.matches("[a-zA-Z]+", text) == false && text.length() > 2) {
    number = text; 
}

当前回答

import java.util.*;

class Class1 {
    public static void main(String[] argh) {
        boolean ans = CheckNumbers("123");
        if (ans == true) {
            System.out.println("String contains numbers only");
        } else {
            System.out.println("String contains other values as well");

        }
    }


    public static boolean CheckNumbers(String input) {
        for (int ctr = 0; ctr < input.length(); ctr++) {
            if ("1234567890".contains(Character.valueOf(input.charAt(ctr)).toString())) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }
}

其他回答

下面的正则表达式可以用来检查字符串是否只有数字:

如果(str.matches(”。*[^ 0 - 9]。*”))或者(str.matches(”。* \ \ d . * "))

如果String包含非数字,上述两个条件都将返回true。在false时,字符串只有数字。

亚当·博德罗吉的略有修改版本:

public class NumericStr {


public static void main(String[] args) {
    System.out.println("Matches: "+NumericStr.isNumeric("20"));         // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("20,00"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("30.01"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("30,000.01"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("-2980"));          // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("$20"));            // Should be true
    System.out.println("Matches: "+NumericStr.isNumeric("jdl"));            // Should be false
    System.out.println("Matches: "+NumericStr.isNumeric("2lk0"));           // Should be false
}

public static boolean isNumeric(String stringVal) {
    if (stringVal.matches("^[\\$]?[-+]?[\\d\\.,]*[\\.,]?\\d+$")) {
        return true;
    }

    return false;
}
}

今天不得不使用这个,所以刚刚发布了我的修改。包括货币、千位逗号或句号符号,以及一些验证。不包括其他货币符号(欧元、分),验证逗号为每三位数。

boolean flag = false;
        System.out.print("Enter String : ");
        String str = new Scanner(System.in).next();

        for (int i = 0; i < str.length(); i++)
        {
            if (str.length() <= 0)
            {
                System.out.println("String length Can't be zero.");
                return;
            }
            char ch = str.charAt(i);
            int c = ch;
            if (c >= 48 && c <= 58)
            {
                flag = true;
            } else
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            System.out.println("input [" + str + "] contains number only.");
        } else
            System.out.println("input [" + str + "] have some non string values in it.");

在这样的典型场景中涉及任何异常抛出/处理是一种糟糕的实践。 因此,parseInt()不是很好,但regex是一个优雅的解决方案,但要注意以下几点: 分数 消极的数字 -十进制分隔符可能因国家而异(例如:','或'。') -有时允许使用所谓的千位分隔符,如空格或逗号,例如12324,1000.355

要处理应用程序中所有必要的情况,您必须小心,但是这个正则表达式涵盖了典型的场景(正/负和分数,用一个点分隔):^[-+]?\d*.?美元\ d + 对于测试,我推荐regexr.com。

这是我的代码,希望对你有所帮助!

 public boolean isDigitOnly(String text){

    boolean isDigit = false;

    if (text.matches("[0-9]+") && text.length() > 2) {
        isDigit = true;
    }else {
        isDigit = false;
    }

    return isDigit;
}