检查,看看是否在两维数组中的元素是某一数据类型的 [英] Check and see if an element in a two-dimensional array is of a certain data type

查看:120
本文介绍了检查,看看是否在两维数组中的元素是某一数据类型的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写检查,看看是否被传递到我实例化类的构造函数的文本文件包含非数字数据的方法。具体地,它的事项如果数据不能被重新psented作为双$ P $。也就是说,字符是不好啦,并整数是。

我至今是:

 私人布尔非数字(双[] []格)抛出异常{
    布尔isNonNumeric = FALSE;    的for(int i = 0; I< grid.length;我++)
        对于(INT J = 0; J<电网[I]。长度; J ++){
            如果(电网[I] [J]!=){
                isNonNumeric = TRUE;
                抛出新ParseException的(NULL,0);
            }其他{
                isNonNumeric = FALSE;
            }
        }
    返回isNonNumeric;
}

我似乎无法找到我应该检查电网的当前索引[i] [j]的反对。据我了解,TYPEOF仅适用于对象。

有什么想法?谢谢你。

编辑:这里是code用于创建双[] []格:

  //创建与文本第一行发现号的二维数组
    //文件。
    格=新的双[(INT)行] [(INT)COL]; //因为铸造为整数
                                                //的尺寸
                                                //电网必须全
                                                //数字。    //使用嵌套的for循环来填充二维数组
    的for(int i = 0; I<排;我++)
        对于(INT J = 0; J<西; J ++){
            如果(scan.hasNext()){
                电网[I] [J] = scan.nextDouble();
                算上++;
            }
        }    //检查,看看行和列乘以总价值
    如果(行* COL!=计数){
        抛出新的DataFormatException();
    }


解决方案

我想出这个样品给你,希望它帮助。

它可以帮助你条目类型缩小到你正在寻找任何类型的。

我entry.txt包括:

 。 1.7 ...我是预定1.1 2.21 2 3222 2.9999黄色1-1 izak。 izak,izak? .. -1.9

我的code:

 公共类ReadingJustDouble {  公共静态无效的主要(字串[] args){    文件f =新的文件(C:\\\\ \\\\用户\\\\ Izak文件\\\\的NetBeansProjects
            +\\\\ ReadingJustString \\\\的src \\\\ readingjuststring \\\\ entry.txt);
    尝试(扫描仪输入=新的扫描仪(F)){
        而(input.hasNext()){
            字符串s = input.next();
            如果(isDouble(S)及&放大器; s.contains()。){
                的System.out.println(Double.parseDouble(S));
            }其他{
            }
        }
    }赶上(例外五){    }
}公共静态布尔isDouble(字符串str){
    双D = 0.0;
    尝试{
        D = Double.parseDouble(STR);
        返回true;
    }赶上(NumberFormatException的NFE){
        返回false;
    }
 }

}

输出:

  1.7
1.1
2.21
2.9999
-1.9

请注意:我的来源如下:

1。 http://www.tutorialspoint.com/java/lang/string_contains。 HTM

2 <一个href=\"http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java\">How检查一个字符串是

数值类型在Java中

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();
    }

解决方案

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 a numeric type in Java

这篇关于检查,看看是否在两维数组中的元素是某一数据类型的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆