0

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();
    }
rwo
  • 19
  • 3
  • A `double` cannot contain non-numeric data. If you have text in a file that is not numeric, the checking has to be done at the time the text is converted to a `double` or before. – ajb Jun 17 '14 at 01:23
  • Thanks, @ajb. That makes sense. So I need to check and see if the data being passed into the double[][] grid is *actually* numeric. However, I'm still unsure of how to do that during parsing. – rwo Jun 17 '14 at 01:24
  • Do you already have code that creates the `double[][]`? What does it look like? – ajb Jun 17 '14 at 01:25
  • The javadoc for [`nextDouble`](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextDouble--) should give you the answer. – ajb Jun 17 '14 at 01:29
  • @ajb Awesome, thank you so much. I believe I know where to go from here: while I'm parsing, use scan.nextDouble(). If it throws an exception, I'll know that I've found a data type that cannot be converted into a double. Does that sound correct? – rwo Jun 17 '14 at 01:31
  • ya, but the question is how you gonna handle the exception? I do not see anywhere in your code that you have used Scanner ? – Kick Buttowski Jun 17 '14 at 01:33
  • @KickButtowski, I plan on catching the exception and simply printing the stack trace, then exiting. This is for a school project, and our professor isn't requiring us to handle the exception (I think. ☺ Also, I declared the scanner in the member variables and instantiated it in the constructor. – rwo Jun 17 '14 at 01:35
  • Oh k but as I said, I do not see anywhere in your code that you used Scanner? – Kick Buttowski Jun 17 '14 at 01:36
  • @KickButtowski, it's declared in my member variables and instantiated in the constructor. – rwo Jun 17 '14 at 01:37
  • oh k so you are just concerned about recognizing double? – Kick Buttowski Jun 17 '14 at 01:38
  • @KickButtowski Correct. I'm concerned by someone attempting to pass in, say the char 'a' to my double[][]. – rwo Jun 17 '14 at 01:38

1 Answers1

3

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

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58