java - 您如何处理从文件上传的数据?例如,计算总和? java

标签 java arrays file

我使用扫描仪将文本文件读入 java 程序。我现在想计算文件中所有整数值的总和。文件结构简单。

Nottinghill_Gate,120
High_Street_Kensignton,100
Gloucester_Road,50
South_Kensignton,200
Sloane_Square,100
Victoria,300
St_James_Park,200
Westminster,100
Embankment,200
Temple,150
Blackfriars,200
Mansion_House,300
Cannon_Street,190
Monument,200
Tower_Hill,160
Aldgate,190
Liverpoool_Street,60
Moorgate,50
Barbican,120
Farrington,130
Kings_Cross_St_Pancras,150
Euston_Square,180
Great_Portland_Street,120
Baker_Street,135
Edware_Road,112
Paddington,115
Bayswater,165

我的代码是

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
 public class DataScanner {
 public static void readFile(String fileName) {
   try {
     Scanner scanner =
       new Scanner(new File(fileName));
     scanner.useDelimiter
       (System.getProperty("line.separator")); 
     while (scanner.hasNext()) {
       parseLine(scanner.next()); 
       //int total =+ distance;
   }


     scanner.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   }
 }

  public static void parseLine(String line) {
   Scanner lineScanner = new Scanner(line);
   lineScanner.useDelimiter("\\s*,\\s*");
   String current = lineScanner.next();
   int distance = lineScanner.nextInt();
   System.out.println("The current station is " + current + " and the destination to the next station is " + distance + ".");
   int total =+ distance;
   System.out.println("The total distance is " + total);
  }

 public static void main(String[] args) {
   if (args.length != 1) {
     System.err.println("usage: java TextScanner2"
       + "file location");
     System.exit(0);
   }
   readFile(args[0]);
 }}

我知道我必须改变

int distance = lineScanner.nextInt();
System.out.println("The current station is " + current + " and the destination to the    next station is " + distance + ".");
int total =+ distance;
System.out.println("The total distance is " + total);

但我不太确定该怎么做。你能帮帮我吗?

最佳答案

变量total 是一个局部变量,为每一行初始化,因此它总是包含每一行的距离。为了求和,您需要将其存储为类变量。此外,您不应该使用静态方法。

public class DataScanner {
  private int total = 0;

  public int getTotal() {
    return total;
  }

  public void readFile(String fileName) { 
    // ...
  }

  public void parseLine(String line) {
    // ...
    total += distance; // update class variable
  } 

  public static void main(String[] args) {
    DataScanner scanner = new DataScanner();
    scanner.readFile(args[0]);
    System.out.println("The total distance is " + scanner.getTotal() + ".");
  }
}

或者,您可以从 parseLine 方法返回每一行的距离,并在 readFile 中对它们求和:

public void readFile(String fileName) {
  // ...
  int total = 0;
  while (scanner.hasNext()) {
    int distance = parseLine(scanner.next());
    total =+ distance;
  } 
  System.out.println("The total distance is " + scanner.getTotal() + ".");
}

public int parseLine(String line) {
  // ...
  return distance;
}

关于java - 您如何处理从文件上传的数据?例如,计算总和? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4610692/

相关文章:

javascript - 在 jQuery/javaScript 中迭代对象数组(时间相关迭代)

当大小超过 742 KB 时,PHP 文件上传失败并出现空白屏幕

java - 循环队列中的 isEmpty() 没有计数方法/

java - XML验证

c# - 错误 : Index was outside the bounds of the array.

file - Shiny 地从上传的数据框中选择特定的列

python - 从文本文件创建字典

java - 如何在 Java 中比较字符串?

java - 带有 MultiTenantConnectionProvider 的 Springboot Multi-Tenancy 总是抛出 org.apache.tomcat.jdbc.pool.PoolExhaustedException :

javascript - Angular Js - 打印具有特定键的数组