Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
哈希映射混乱。读取/写入文件。JAVA_Java_File_Hashmap_Filewriter - Fatal编程技术网

哈希映射混乱。读取/写入文件。JAVA

哈希映射混乱。读取/写入文件。JAVA,java,file,hashmap,filewriter,Java,File,Hashmap,Filewriter,迄今为止的代码: public class test1 { public static void main(String[] args) throws IOException {

迄今为止的代码:

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  Marks2: 21.2
 - - - - - -
 25212 Greg
 Marks: 70.5  Marks2: 23.43
 - - - - - -         
那么我能做些什么来补充呢?我假设我必须为新的文本文档添加一个新的Hashmap?但当我尝试做所有这些的时候,它从来没有完全起作用

国际关系学院学生:

25987 Alan
25954 Betty
25654 Chris
25622 David                                                                                                      

添加新标记时,使用此选项将其添加到现有标记中:

String key = arg[0];
String secondMarks = arg[1];

String theMarks = names.get(key);
theMarks = theMarks + " Marks2: " + secondMarks;
names.put(key, theMarks);

我想我理解你的问题,如果这是错误的,请告诉我

要求是要有该人在自己的线路上收到的所有标记

System.out stream中有两个打印功能

print和println

arg = line.split(" ");
writer.println(arg[0] + " " + names.get(arg[0]));

writer.print("Marks: " + arg[1]);
for(int i = 2; i < args.length; i++){
    writer.println(" Marks" + i + ": " + arg[i]);
}

writer.println("\n- - - - - -");
arg=line.split(“”);
writer.println(arg[0]+“”+names.get(arg[0]);
writer.print(“标记:+arg[1]);
对于(int i=2;i
我认为您对字符串的操作太多了。如果您将有更多的marks文件以类似的方式处理,那么字符串操作可能会增加,这可能会降低代码的可读性,并为错误提供更多的空间。我认为下面是一个更好的方法

您可以使用以下结构创建MarksRecord类

public class MarksRecord {
   private String subject; // or whatever this variable name should be.
                           // in your case it should hold value marks1.
   private double marks;

}
类似地,您可以创建一个不可变的Student/类似类,如下所示。这可能是一个值类,其中包含基于每个文件中读取的第一个数字的equals和hashCode方法。我猜是卷号或类似的数字能够以一种独特的方式识别学生

public final class Student {
    private final String rollNumber;
    private final String name;

    // equals, hashCode, and other methods.
}
然后在你的主要方法中,你可以有一个

Map<Student, ArrayList<MarksRecord>>
Map
。或者,您也可以使用

Map<String, ArrayList<MarksRecord>>
Map
其中第一个字符串是学生记录的卷号


这样,每当您有一个新的标记文件时,您的数据结构将能够容纳它。

您也可以执行以下操作

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.util.HashMap;
导入java.util.Map;
导入java.util.Objects;
公共类标记处理器{
私有最终映射=新HashMap();
公共静态void main(字符串[]args){
String fileName=“file1.txt”;//将其更改为您的特定文件。
MarksProcessor MarksProcessor=新的MarksProcessor();
marksProcessor.processFile(文件名,0);
fileName=“file2.txt”;
marksProcessor.processFile(文件名,1);
marksProcessor.writeData();
}
私有void进程文件(字符串文件名,int-marksIndex){
尝试(/*在此处指定您的读卡器资源*/){
//读取第一条记录并获取卷号、名称和标记。
String roll=“valueYouGot”;
double value=0.0;//读取的值。
记录=map.get(roll);
//如果记录为空,则需要创建一个
//把它放到地图上。
//record.updateMarks(marksndex,value);
}
}
私有无效写入数据(){
//如果需要将其写入文件/流,请创建一个writer。
对于(Map.Entry:Map.entrySet()){
字符串roll=entry.getKey();
Record=entry.getValue();
if(记录!=null){
String name=record.getName();
双标记s1=记录.getMarks(0);
双标记2=记录.getMarks(1);
//现在你有了所有的值。打印它们
//不管你喜欢什么,无论你喜欢哪里。
}
}
}
静态课堂记录{
私有字符串名称;
专用双精度[]标记=新双精度[2];
记录(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共双标记(整数索引){
如果(索引<0 | |索引>1)
抛出新的IllegalArgumentException(“索引应为0或1,但”
+“提供的指数为”+指数);
返回标记[索引];
}
公共void更新标记(整数索引,双精度值){
如果(索引<0 | |索引>1)
抛出新的IllegalArgumentException(“索引应为0或1,但”
+“提供的指数为”+指数);
标记[索引]=值;
}
@凌驾
公共字符串toString(){
返回“输入输出的方式”;
}
}
}

这不涉及标记第二部分的新文本文件。或者我不明白你的代码IRStudents文件是什么样子的?卷号或者任何最有意义的描述它的值。谢谢你的回复。我只有两个标记文件可供使用。但是你的方法可能更好,因为我需要对分数进行除法(得到平均值)。或者说,用我的方式还是可能的?是的,用你的方式也是可能的。尽管我强烈建议你不要走那条路。我需要写另一个答案来解释如何做到这一点。给我几分钟,酷。所以我将跳过添加大量字符串操作的答案。当你说equals方法时,这是检查学生ID是否相等吗?@Chad再次非常感谢。现在,我们正在检查代码,试图理解它。processFiles方法实际上是一个伪代码。您知道,当您第一次获取卷的记录时,它将为空。如果找不到,您必须构建一个记录并将其放入地图中。但那部分是你要完成的。
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";
        }

    }


}