简体   繁体   English

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

[英]how do you work with data uploaded from files? e.g., calculate the sum? java

I have used scanner to read a text file into a java program. 我已经使用扫描仪将文本文件读入Java程序。 I now want to calculate the sum of all integer values in file. 我现在要计算文件中所有整数值的总和。 The structure of file is simple. 文件的结构很简单。

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

my code is 我的代码是

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]);
 }}

I know I have to change 我知道我必须改变

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);

But I am not quite sure how to. 但是我不太确定该怎么做。 Could you please help me please? 你能帮我吗?

The variable total is a local variable , initialized for each line, thus it will always contain the distance of each line. total变量是局部变量 ,为每行初始化,因此它将始终包含每行的距离。 In order to have a sum, you need to store it as a class variable . 为了获得总和,您需要将其存储为类变量 Also, you shouldn't be using static methods. 另外,您不应该使用静态方法。

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() + ".");
  }
}

Alternatively, you could return each line's distance from the parseLine method, and sum them all in readFile : 或者,您可以返回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;
}

Your mistake is in parseLine method. 您的错误在于parseLine方法。

int total =+ distance;

You are not incrementing, but just reseting your total 您不是在增加,而是在重置总数

remove the int 移除int

define 限定

public static int total = 0;

so your code would look like: 因此您的代码如下所示:

public class DataScanner {
  public static int total = 0;
  public static void readFile(String fileName) {

暂无
暂无

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

相关问题 如何从 MacOS 卸载 Java 版本(例如,openjdk 15)? - How do you uninstall a version of Java (e.g., openjdk 15) from MacOS? 你如何让java在两个范围内创建一个随机数(例如5-10和12-17) - How do you have java create a random number within two ranges (e.g. 5-10 and 12-17) Java泛型和数组类型,不是你想的(例如来自泛型类型的数组) - Java generic and array types, no not what you are thinking (e.g. arrays from generic types) 如何为Android应用创建标题屏幕? 例如,当应用程序像Facebook一样加载时 - How do you create a title screen for an android app? E.g. When the app is loading like Facebook 如何安全地为要启动的后台应用程序提供敏感信息(例如密码)? - How do you securely provide a sensitive info (e.g. password) to a background application to be started? 从外部数据源修改 Java JTable AbstractTableModel(例如从套接字读取的数据) - Modify Java JTable AbstractTableModel from external data source (e.g. data read from a socket) Java - 将一个文件中的内容逐块(例如 8 字节)交替写入多个文件 - Java - Write content from one file chunk by chunk (e.g. 8 Bytes) alternately into multiple files 用Java重新排列对象(例如,用于Minecraft NBT文件) - Rearranging objects in Java (e.g. for Minecraft NBT files) Java:如何获得一个月中x天的日期(例如2012年2月的第三个星期一) - Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012) 如何在Java中查看内置类的源代码(例如BigInteger等)? - How do I view source code of built-in classes in Java (e.g. BigInteger etc.)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM