检查二维数组中的元素是否具有某种数据类型 [英] Check and see if an element in a two-dimensional array is of a certain data type
问题描述
我到目前为止是:
private boolean nonNumeric(double [] [] grid)throws Exception {
boolean isNonNumeric = false; (int i = 0; i< grid.length; i ++)
for(int j = 0; j< grid [i] .length; j ++){
if(grid [i] [j]!=){
isNonNumeric = true;
抛出新的ParseException(null,0);
} else {
isNonNumeric = false;
}
}
return isNonNumeric;
}
我似乎找不到我应该检查当前的网格索引[我] [j]反对。据我了解,typeOf只适用于对象。
任何想法?谢谢。
编辑:以下是用于创建double [] []网格的代码:
//使用从文本第一行
//文件中找到的数字创建一个2D数组。
grid = new double [(int)row] [(int)col]; //从
//以// b // $ // //的尺寸为整数,必须是整数
//数字。
//使用嵌套for循环填充2D数组
for(int i = 0; i
for(int j = 0; j< ; col; j ++){
if(scan.hasNext()){
grid [i] [j] = scan.nextDouble();
count ++;
}
}
//查看行和列是否乘以总值
if(row * col!= count){
抛出新的DataFormatException();
}
对于你,希望它有所帮助。
它可以帮助您将您的条目类型缩小到您要查找的任何类型。
我的entry.txt包括:
。 ... 1.7我是书1.1 2.21 2 3222 2.9999 yellow 1-1 izak。 izak,izak? .. -1.9
我的代码:
public class ReadingJustDouble {
public static void main(String [] args){
文件f =新建文件(C: \\Users\\Izak\\Documents\\NetBeansProjects
+\\ReadingJustString\\src\\readingjustingstring\\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(异常e){
}
}
public static boolean isDouble(String str){
double d = 0.0;
try {
d = Double.parseDouble(str);
返回true;
} catch(NumberFormatException nfe){
return false;
}
}
}
输出:
1.7
1.1
2.21
2.9999
-1.9
注意:我的来源如下
1。 http://www.tutorialspoint.com/java/lang/ string_contains.htm
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 numeric in Java
这篇关于检查二维数组中的元素是否具有某种数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!