Check and see if an element in a two-dimensional array is of a certain data type

≯℡__Kan透↙ 提交于 2019-12-25 05:49:05

问题


I'm writing a method that checks to see if the text file being passed into the constructor of my instantiable class contains non-numeric data. Specifically, it matters if the data cannot be represented as a double. That is, chars are not okay, and integers are.

What I have so far is:

private boolean nonNumeric(double[][] grid) throws Exception {
    boolean isNonNumeric = false;

    for (int i = 0; i < grid.length; i++)
        for (int j = 0; j < grid[i].length; j++) {
            if (grid[i][j] !=  ) {
                isNonNumeric = true;
                throw new ParseException(null, 0);
            } else {
                isNonNumeric = false;
            }
        }
    return isNonNumeric;
}

I cannot seem to find what I should be checking the current index of grid[i][j] against. As I understand it, typeOf only works on objects.

Any thoughts? Thank you.

Edit: Here is the code used to create the double[][] grid:

// Create a 2D array with the numbers found from first line of text
    // file.
    grid = new double[(int) row][(int) col]; // Casting to integers since
                                                // the dimensions of the
                                                // grid must be whole
                                                // numbers.

    // Use nested for loops to populate the 2D array
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++) {
            if (scan.hasNext()) {
                grid[i][j] = scan.nextDouble();
                count++;
            }
        }

    // Check and see if the rows and columns multiply to total values
    if (row * col != count) {
        throw new DataFormatException();
    }

回答1:


I come up with this sample for you, hope it helps.

It helps you to narrow down your entries types to any type that you are looking for.

My entry.txt include :

. ... 1.7 i am book 1.1 2.21 2 3222 2.9999 yellow 1-1 izak. izak, izak? .. -1.9

My code:

public class ReadingJustDouble {

  public static void main(String[] args) {

    File f = new File("C:\\Users\\Izak\\Documents\\NetBeansProjects"
            + "\\ReadingJustString\\src\\readingjuststring\\entry.txt");
    try (Scanner input = new Scanner(f);) {
        while (input.hasNext()) {
            String s = input.next();
            if (isDouble(s) && s.contains(".")) {
                System.out.println(Double.parseDouble(s));
            } else {
            }
        }
    } catch (Exception e) {

    }
}

public static boolean isDouble(String str) {
    double d = 0.0;
    try {
        d = Double.parseDouble(str);
        return true;
    } catch (NumberFormatException nfe) {
        return false;
    }
 }

}

Output:

1.7
1.1
2.21
2.9999
-1.9

Note: my sources are as follows

1.http://www.tutorialspoint.com/java/lang/string_contains.htm

2.How to check if a String is numeric in Java



来源:https://stackoverflow.com/questions/24254431/check-and-see-if-an-element-in-a-two-dimensional-array-is-of-a-certain-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!