简体   繁体   中英

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

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.

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.

However, if the OP does not have a choice, here is a variant of the test program that shows how to do it:

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

Using a String[] is the wrong approach to this overall. 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]*$". 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);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM