gpt4 book ai didi

java - HashMap 困惑。读取/写入文件。 java

转载 作者:行者123 更新时间:2023-12-02 04:50:39 25 4
gpt4 key购买 nike

到目前为止的代码:

public class test1 {                                                                                                           

public static void main(String[] args) throws IOException {
//declare reader and writer
BufferedReader reader = null;
PrintWriter writer = null;

//hash maps to store the data
HashMap<String, String> names = new HashMap<String, String>();


//read the first file and store the data
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
String line;
String[] arg;

while ((line = reader.readLine()) != null) {
if (!line.startsWith("-")) {
arg = line.split(" ");


names.put(arg[0], arg[1]);


}
}
reader.close();

//read the second file, merge the data and output the data to the out file
writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("Marks.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
writer.println(arg[0] + " " + names.get(arg[0]));
writer.println("Marks: " + arg[1]);
writer.println("- - - - - -");
}


writer.flush();
writer.close();
reader.close();
}
}

因此文本文件中的输出如下所示:

 25220 Fiona
Marks: 68.3
- - - - - -
25212 Greg
Marks: 70.5
- - - - - -

我有另一个文本文件,其中包含另一组标记,其布局与第一个标记文件相同。

现在我想向数据集添加一组新的标记,因此它应该如下所示:

 25220 Fiona
Marks: 68.3 Marks2: 21.2
- - - - - -
25212 Greg
Marks: 70.5 Marks2: 23.43
- - - - - -

那么我可以做什么来添加呢?我想我必须为新的文本文档添加一个新的 HashMap ?但当我尝试做所有这些事情时,它从来没有完全发挥作用。

IR 学生:

25987 Alan
25954 Betty
25654 Chris
25622 David

最佳答案

您也可以执行以下操作。

package toBeDeleted;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MarksProcessor {


private final Map<String, Record> map = new HashMap<>();

public static void main(String[] args) {
String fileName = "file1.txt"; // change it to your specific file.
MarksProcessor marksProcessor = new MarksProcessor();
marksProcessor.processFile(fileName, 0);
fileName = "file2.txt";
marksProcessor.processFile(fileName, 1);
marksProcessor.writeData();

}

private void processFile(String fileName, int marksIndex) {
try(/*specify your reader resources here*/) {
// read the first record and get rollNumber, name and marks.
String roll = "valueYouGot";
double value = 0.0; // the value you read.
Record record = map.get(roll);
// if record is null, you need to create one
// and put it into the map.
//record.updateMarks(marksndex, value);
}
}

private void writeData() {

// if this needs to be written to a file/stream, create a writer.
for (Map.Entry<String, Record> entry : map.entrySet()) {
String roll = entry.getKey();
Record record = entry.getValue();
if (record != null) {
String name = record.getName();
double marks1 = record.getMarks(0);
double marks2 = record.getMarks(1);
// Now you have all the values. Print them
// however you like. Wherever you like.
}
}
}

static class Record {
private String name;
private double[] marks = new double[2];


Record(String name) {
this.name = name;
}

public String getName() {
return name;
}

public double getMarks(int index) {
if (index < 0 || index > 1)
throw new IllegalArgumentException("index should be 0 or 1 but"
+ " the supplied index was " + index);
return marks[index];
}
public void updateMarks(int index, double value ) {
if (index < 0 || index > 1)
throw new IllegalArgumentException("index should be 0 or 1 but"
+ " the supplied index was " + index);
marks[index] = value;
}


@Override
public String toString() {
return "the way you want to type your output";
}

}


}

关于java - HashMap 困惑。读取/写入文件。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com