从args读取文件时捕获错误(java)

丹尼尔

有人可以向我解释为什么我遇到catch错误吗?

我正在尝试从传递给args的文件中读取值(数字)。

我不太了解问题的根源。

import java.util.Scanner;//               Import the Scanner class to read text files
import java.io.File;//                    Import the File class
import java.io.FileNotFoundException;//   Import this class to handle errors
import java.io.*;

public class main extends GeneralMethods {
  public static void main(String[] args) {
    if (args.length <= 1 || args.length > 2) {
      println("Error, usage: software must get two input files");
      System.exit(1);
    }

    String file_name1 = args[0]; // data to insert
    String file_name2 = args[1]; // data to check

    File data_to_insert = new File(file_name1);
    File data_to_check = new File(file_name2);

    Scanner input = new Scanner(System.in); // Create a Scanner object

    println("Enter hashTable size");
    int hashTable_size = input.nextInt(); // Read hashTable_size from user

    println("Enter num of hashing function");
    int num_of_has = input.nextInt(); // Read num of hashing from user

    hashTable T = new hashTable(hashTable_size);
    println("hashTable before insert values\n");
    T.printHashTable();
    input.close();

    int i = 0;
    try {
      input = new Scanner(data_to_insert);
      String data;
      while ((data = input.next()) != null) {
        T.set(i, Integer.parseInt(data));
        i++;
      }
      input.close();
    } catch (Exception e) {
      System.out.println("\nError: Reading, An error occurred while reading input files. Check your input type");
      e.printStackTrace();
    }
    T.printHashTable();
  }
} 

这是我的输出

哪个显示捕获错误

hashTable before insert values

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Error: Reading, An error occurred while reading input files. Check your input type
java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1478)
        at main.main(main.java:36)
[1,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
杰姆斯

在这一行,

while ((data = input.next()) != null) 

如果没有更多数据,Scanner的next()方法不会返回null,而是抛出您得到的NoSuchElementException。

使用它代替检查更多数据:

while ((input.hasNext()) {
    data = input.next();
    //...
}

如您所料,方法hasNext()返回true或false。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

读取文件时Java错误

Node JS:如何在读取文件时捕获单个错误,以防在 Promise.all 上读取多个文件?

在python中读取文件时捕获错误的最佳位置是什么?

从文件读取数组元素时发生NullPointerException错误-Java

读取xml文件时出现Java异常错误

读取pandas中的csv文件时出错[CParserError:标记数据时出错。C错误:捕获到缓冲区溢出-可能是格式错误的输入文件。

从文件Java读取错误的字符

未捕获的类型错误:无法读取元素 null 的属性“文件”

未捕获的引用错误(无法读取JSON文件)

在Python 3中逐行读取文件时捕获UnicodeDecodeError异常

读取 CBOR 文件时出现类型错误

读取文件时发生InvalidClassException serialVersionUid错误

读取文件C ++时出现奇怪的错误

读取文件元数据时的DateTime错误

读取文件时valgrind中的错误-C

读取文件时scanf给出分段错误

读取文件时出现NumberFormatException错误

从txt文件读取时的错误处理

读取 .npy 文件时检索错误

错误未捕获类型错误:无法在单独的文件中读取 null 的属性(读取“addEventListener”)

尝试从邮筒读取时出现C读取文件错误87

在Java中:从网络路径读取时出现“打开文件过多”错误

在Java中读取2GB xml文件时出现OOM错误

尝试从Java中的HDFS读取文件时出现“错误的FS ...预期:file:///”

如何在Java中读取文件时摆脱Invalid Mark错误?

Java在尝试读取文本文件时遇到运行时错误

“未捕获的类型错误:单击数字按钮时无法读取未定义的属性(读取 'toString')

Java-从CSV文件读取错误

读取大文件错误“ outofmemoryerror”(java)