java - 如何创建一个使用数据引用保存 ArrayList 的类?
问题描述
我有一个包含此类信息的 30 mb txt 文档(其中,首先是日期、时间、百万分之一秒数、3 个用“;”分隔的价格和一个交易)文件:https://drive. google.com/file/d/1-ky0EM75xvDXdHBcFnZYjq7D9FwKy50M/view?usp=sharing
20200429 050000 2500000;8843;8841.75;8843;1
20200429 050000 5740000;8843;8841.75;8843;1
20200429 050000 5740000;8843.25;8841.75;8843.25;1
每个数据都存储在一个名为“Dato”的类中,并且有一个数据集合“ColeccionDatos”,其中引用了每个数据,但是,在创建实例并尝试读取文件“NQ20200429 -30.txt”时“其中包含所有信息,发生 Java.util.MismatchException 错误。我已经尝试了很长时间,但无法解决,感谢您的帮助。这是代码。
public class Dato
{
private String fecha;
private String hora;
private int diezmill;
private double last;
private int transaccion;
public Dato (String fecha, String hora, double last, int trans){
this. fecha = fecha;
this. hora = hora;
this. last = last;
this. transaccion = transaccion;
}
public String getFecha(){
return this.fecha;
}
public String getHora () {
return this.hora;
}
public int getDiezmill () {
return this. diezmill;
}
public double getLast () {
return this. last;
}
public int getTransaccion () {
return this.transaccion;
}
public String toString () {
String ret = "";
ret += fecha + "";
ret += hora + "";
ret += diezmill + "";
ret += last + "";
ret += transaccion + "";
return ret;
}
}
//第二个类的代码是
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
public class ColeccionDatos
{
protected ArrayList <Dato> al;
public ColeccionDatos () {
al = new ArrayList <> ();
}
public void leerActivo (String NombreActivoNQ) throws FileNotFoundException {
Scanner archivo = new Scanner (new File (NombreActivoNQ)); //In this line the file is processed
while (archivo.hasNextLine()){
String linea = archivo.nextLine();
linea = linea.replace (";", " ");
Scanner sl = new Scanner (linea);
String fecha = sl.next();
String hora = sl.next();
int diezmill = sl.nextInt();
double bid =sl.nextDouble();
double ask = sl.nextDouble(); //this line throws the exception
double last = sl.nextDouble();
int transaccion = sl.nextInt();
Dato d = new Dato (fecha, hora, last, transaccion);
al.add(d);
}
}
}
解决方案
你在 Dato 构造函数中犯了一个错误,它应该this. transaccion = trans;
是this. transaccion = transaccion;
您能否分享您的 NQ20200429 -30.tx 文件,因为我使用给定的示例运行您的代码并且我没有遇到 Java.util.MismatchException 错误,可能输入文件有问题
推荐阅读
- flutter - 如何更新 Flutter MobX ObservableList 中的对象?
- arrays - 数据库:
调试 VS 代码时在数组中 - android-studio - Android Studio 不会显示任何错误
- function - 如何为类方法制作测试功能?
- python - pandas:当您需要按顺序验证数据时,如何避免遍历行?
- javascript - 使用 Javascript 删除隐藏选项卡上所需的标签
- c++ - 产生错误:“g++:错误:bin/main.o:没有这样的文件或目录”
- bash - 如何从 Python 的 git repo 运行执行命令?壳牌 + 码头工人
- python - Python - 解析 Ifconfig 文件
- swift - 上下文闭包类型 '() -> Void' 需要 0 个参数,但在闭包体中使用了 1