繁体   English   中英

读取文本文件中的整数并使用扫描仪和异常存储到数组

[英]Read text file for integers and storing to array using scanner and exceptions

我正在尝试遍历文本文件中的整数,并将找到的整数存储到数组中。

使用try-catch来确定哪些单词是整数,哪些单词未使用InputMismatchException,从输入流中删除非整数字符串。 以及文件中空白行的NoSuchElementException。 我的主要问题是在第二种方法:o中存储整数并将这些整数打印在数组中。 看来我的循环也将非整数也记录为null。 它们不应该存储在数组中。

public static void main(String[] commandlineArgument) {
      Integer[] array = ReadFile6.readFileReturnIntegers(commandlineArgument[0]);
      ReadFile6.printArrayAndIntegerCount(array, commandlineArgument[0]);
   }

   public static Integer[] readFileReturnIntegers(String filename) {
      Integer[] array = new Integer[1000];
      // connect to the file
      File file = new File(filename);
      Scanner inputFile = null;
      try {
         inputFile = new Scanner(file);
      }
      // If file not found-error message
      catch (FileNotFoundException Exception) {
         System.out.println("File not found!");
      }
      // if connected, read file
      if (inputFile != null) {
      // loop through file for integers and store in array
         while (inputFile.hasNextLine()) {
            for(int i = 0; i<array.length; i++)
            {
               try{
                  array[i] = inputFile.nextInt();
               }
               catch(InputMismatchException excep1)
               {
                  String word = inputFile.next();
               }
               catch(NoSuchElementException excep2){
               }
            }
         }
      }
      return array;
   }

   public static void printArrayAndIntegerCount(Integer[] array, String filename) {
   //prints number of integers from file
   //prints each integer in array
   }
}

假设您从文件中读取整数的所有逻辑都是正确的,并且还希望这是一种家庭作业。 尽管以下实现不是正确的方法,但它只是解决了您的目的。 我们在这里要做的就是迭代数组中的所有元素,直到它达到null为止,并继续将它们写入缓冲区。

public static void printArrayAndIntegerCount(Integer[] array, String filename) {
   StringBuilder sb = new StringBuilder();
   int count = 0;
   for(Integer i : array) {
      if(i != null) {
          count++;
          sb.append("index = ").append(i).append(", element = ").append(array[i]).append("\n");
      } else {
          break;
      }
   }
   System.out.println("number of integers in file \""+filename+"\" = "+count);
   System.out.println(sb);
}

将您的catch语句替换为:

catch(InputMismatchException excep1)
{
      String word = inputFile.next();
      i-=1;
}

如果发现一个单词,则要递增数组计数器 我已经进行了自己的测试,因此可以解决您的问题。

 public static void printArrayAndIntegerCount(Integer[] array, String filename) {
       String message = "";
       int i = 0;
       while(i < array.length && array[i]!=null){
           message = message + "index = "+i+", element = "+array[i]+"\n";
           i+=1;
        }
       System.out.println("number of integers in file \""+filename+"\" = "+i);
       System.out.println(message);
}

第一种方法采用的方法有些缺陷,因为无论是否读取整数,都将递增i变量。

因此,例如,如果文件如下所示:

4
Hello
5
e
7

数组的开始看起来像

[4, null, 5, null, 7...]

因此,您将得到一个大小为1000的数组,该数组在其中不可预测的位置为null。

更好的方法是:

  • 保留一个单独的count变量,该变量表示您实际读取了多少个整数。
  • 在索引count而不是在i处将项目添加到数组中(因为i只是说您已经看过多少行,而count会告诉您遇到了多少个整数)。
  • 当您读完它们后,
    • count变量传递给打印数组的方法(因此它只知道第一个count项),或者
    • 只需将整个数组复制到一个新的大小count数组中即可。

将其合并到您的代码中的示例:

if(inputFile != null) {
    // the number of integers we've read so far
    int count = 0;

    // loop through file for integers and store in array
    while(inputFile.hasNextLine()) {
        for(int i = 0; i < array.length; i++) {
            try {
                array[count] = inputFile.nextInt();
                count++;
            } catch(InputMismatchException excep1) {
                String word = inputFile.next();
            } catch(NoSuchElementException excep2) {
            }
         }
     }
}

然后复制到正确大小的数组中,

Integer[] newArray = new Integer[count];
for(int i = 0; i < count; i++) {
    newArray[i] = array[i];
}

然后只返回newArray而不是array

然后,您的打印方法将具有与您期望的相同的签名和功能:

public static void printArrayAndIntegerCount(Integer[] array, String filename) {
    for(int i = 0; i < array.length; i++) {
        // print the number at array[i], and whatever else you want to print
    }
}

这可能是更好的方法,因为您仍然可以使所有方法签名保持相同,并且无需弄乱返回多个变量或更改全局状态。


或者,如果您不想将相关位复制到新数组中,则可以将count变量以某种方式传递给第二种方法,然后执行类似的操作

for(int i = 0; i < count; i++) {
    System.out.println("\tindex = " + i + ", element = " + array[i]);
}

关键的区别在于您要迭代count ,而不要迭代array.length

您将需要找到一种方法来从第一个方法连同数组(或者可能在某个地方设置静态变量)中将其返回,然后需要将第二个方法的签名更改为

public static void printArrayAndIntegerCount(Integer[] array, int count, String filename) {
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM