【问题标题】:Summing a list of numbers from a text document对文本文档中的数字列表求和
【发布时间】:2016-04-02 09:09:25
【问题描述】:

我们的目标是创建一个程序,该程序可以扫描带有数字列表的文档并将数字列表求和。该程序采用两个命令行参数。第一个参数告诉求和多少行。第二个参数告诉读取哪一行。例如,如果 args[1]=2 告诉您每隔一行阅读一次。或者如果 args[1]=3 告诉您每三行阅读一次。 到目前为止,这是我的代码:

import java.util.Scanner;

import java.io.*;

public class Lab8{  

    public static void main(String [] args)throws IOException {

        int intCount = 0;

        int sum=0;  

        File myFile;

        myFile=new File("nums.txt");

        Scanner in=new Scanner(myFile);

        if (args.length!=2){

            System.out.println("ERROR:NEEDS TWO CLA'S");

        }else{
            for(int i=0;i<Integer.parseInt(args[0]);i++){
                int x=in.nextInt();
                sum=sum+x;
            }

        }
            System.out.println(sum);        


    }

我收到以下错误消息:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Lab8.main(Lab8.java:33)

如何修复我的代码?谢谢!

【问题讨论】:

    标签: java command-line-arguments


    【解决方案1】:

    您需要检查扫描仪是否有任何要阅读的内容

    Scanner.hasNextInt()
    

    在您阅读之前,otherwise it will throw the exception you're seeing

    至于“读取每 X 行”,您必须创建一个循环来读取您想要跳过的行数,然后再读取您想要存储的行数。

    【讨论】:

      【解决方案2】:

      类似的东西(我没有测试解决方案)

      public static void main(String[] args) throws IOException {
      
          // Check number of parameters at the begening
          // it is not needed to open file if there is no appropriate number of parameters
          if (args.length != 2) {
              System.out.println("ERROR: Need two parameters");
              return; // Or System.exit(1);
          }
      
          File myFile = new File("nums.txt");
          Scanner in = new Scanner(myFile);
      
          int totalLines = Integer.parseInt(args[0]);
          int skipLines = Integer.parseInt(args[0]);
          int currentLine = 0;
          int sum = 0;
      
          while (in.hasNext() && currentLine < totalLines) {
              int num = in.nextInt();
              if (currentLine % skipLines == 0) {
                  sum += num;
              }
          }
      
          System.out.println(sum);
      }
      

      【讨论】:

      • 这段代码编译并运行,但不管命令行参数如何,它总是打印出 48933。我需要做的就是找出while循环输出正确答案的一些逻辑。感谢您的帮助!
      【解决方案3】:

      让我知道这是否达到了您的要求: *请注意,如果您的文本文件与您的程序不在同一目录中,则需要列出完整的文件路径

      public class Lab8{  
          public static void main(String [] args)throws IOException {
      
      
               int lineCount = Integer.parseInt(args[0]);
               int skipCount = Integer.parseInt(args[1]);
      
              int locaCount = 0;
              int sum=0;  
              File myFile;
      
      
              myFile=new File("nums.txt");
      
              Scanner in=new Scanner(myFile);
      
              if (args.length!=2){
      
                  System.out.println("ERROR:NEEDS TWO CLA'S");
      
              }else{
                  for(int i=0;i<lineCount; i += skipCount){
                      int x=in.nextLine();
                      sum+= x;
                  }
      
              }
                  System.out.println(sum);        
      
      
          }
      

      【讨论】:

      • 这段代码编译、运行并打印出一个总和,但不是正确的答案。该代码比我的原始代码工作得更好,因为它不会引发错误。感谢您的帮助!
      • @VictoryGreen 试试这个我不知道为什么我从第一个版本的 skipLine 中减去了一个。
      • 这修复了程序,以便在第二个命令行参数等于 1 (args[1]=1) 时输出正确答案。当 args[1] 的值大于 1 时,我仍然需要修复它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2013-09-14
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多