简体   繁体   English

如何在String []中区分真实字符串和整数值?

[英]How to make difference in String[] between real strings and integer values?

I'm developing an app where I have 我正在开发一个应用程序

String[][] datos;

I need to make a difference between all string and integer values there for one method. 我需要为一种方法在所有字符串和整数值之间做出区别。 for example: 例如:

datos[0][2]: "hi"
datos[1][1]: "3"
datos[2][2]: "a"
datos[3][0]: "25"

in this case I only need 3 and 25 values, but I can't make it doing 在这种情况下,我只需要3和25个值,但我做不到

Integer.parseInt(datos[1][1]);

for each value. 每个值。

In my code I only made specific cases, but I want to make all cases in the first if sentence. 在我的代码中,我只列出了特定的情况,但是我想在第一个if语句中列出所有情况。

for(int f=2;f<maxf;f++)
    for(int c=3;c<maxc;c++){
        if((datos[f][c]!= "A")&&(datos[f][c]!= "-")&&(datos[f][c]!= "")){
            nota= Integer.parseInt(datos[f][c]);
            if(nota>3)
                aprobados.set(f-2, aprobados.get(f-2)+1);
        }
    }

I agree with the objections to using String[][] for mixed data. 我同意将String[][]用于混合数据的反对意见。

However, if the OP does not have a choice, here is a variant of the test program that shows how to do it: 但是,如果OP没有选择,则以下是测试程序的变体,显示了如何执行此操作:

public class Test {
  public static void main(String[] args) {
    int maxf = 4;
    int maxc = 3;
    String[][] datos = new String[maxf][maxc];
    datos[0][2] = "hi";
    datos[1][1] = "3";
    datos[2][2] = "a";
    datos[3][0] = "25";

    for (int f = 0; f < maxf; f++)
      for (int c = 0; c < maxc; c++) {
        if (datos[f][c] != null) {
          try {
            int nota = Integer.parseInt(datos[f][c]);
            System.out.println(nota);
          } catch (NumberFormatException e) {
            // Deliberately empty
          }
        }
      }
  }
}

Create a new method aside : 除了创建一个新方法:

public boolean isNumeric (String s){
    try{
        Double.parseDouble(s);
        return true;
    } catch (Exception e) {
        return false;
    }
}

This will return true if the cast is possible, otherwise false is returned 如果可以强制转换,则返回true,否则返回false

Using a String[] is the wrong approach to this overall. 总体而言,使用String[]是错误的方法。 Instead you should use a Class of your own so you don't have to worry about where in your array you have what now. 相反,您应该使用自己的类,这样就不必担心数组中现在的位置。

You can check if a string contains only digits by using the regular expression "^[0-9]*$". 您可以使用正则表达式“ ^ [0-9] * $”检查字符串是否仅包含数字。 I would prefer not to use exception catching. 我宁愿不使用异常捕获。 I believe regex matching would be more efficient, like this: 我相信正则表达式匹配会更高效,如下所示:

public static void main(String []args){
    int maxf = 4;
    int maxc = 3;
    String[][] datos = new String[maxf][maxc];
    datos[0][2] = "hi";
    datos[1][1] = "3";
    datos[2][2] = "a";
    datos[3][0] = "25";

    for(int f = 0; f < maxf; f++){
        for(int c = 0; c < maxc; c++){
            if(datos[f][c] != null && datos[f][c].matches("^[0-9]*$")){
                int nota = Integer.parseInt(datos[f][c]);
                System.out.println("datos[f][c] = " + nota);
                // if(nota>3)
                //     aprobados.set(f-2, aprobados.get(f-2)+1);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据两个字符串的整数值查找Map中的差异? - How to find the difference in a Map between two Strings based on their integer values? integer 和字符串比较在优化上有什么区别吗? - Is there any difference in optimization between integer and string comparison? Linkedhaspmap 和linkedhashmap 的区别<Integer,String> - Difference between linkedhaspmap and linkedhashmap<Integer,String> 字符串索引两个字符串之间的差异 - String index of Difference between two Strings #strings 和标准 String 方法之间的区别 - Difference between #strings and standard String methods 如何找到两个整数数组之间的差异? - How to find difference between two integer arrays? 如何将字符串拆分为整数,符号和字符串,然后存储整数值以用于算术运算 - How can i split a string into integers, symbols and strings, and then store the integer values to use for arithmetic operation 如何解析字符串列表并将值添加到 Map<string,integer> 使用 Lambda</string,integer> - How To Parse A List Of Strings And Add Values to a Map<String,Integer> Using Lambdas 我该如何计算两个字符串之间的字符差,并使其在Java中相等? - How can I calculate the difference of chars between two strings and make them equal in Java? int和Integer之间的区别 - Difference between int and Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM