从文本文件创建矩阵-java

从文本文件创建矩阵-java,java,matrix,Java,Matrix,我有一个文本文件,其中包含以下格式的数据: word:filename:wordCount 我想创建一个矩阵,其值如下: the length of the matrix is same the number of the words in the file the width is the number of the files. 例如: apple:file1:2 apple:file3:4 cat:file1:3 tea:file2:5 ugly:file4:3 长度=5 宽度=4

我有一个文本文件,其中包含以下格式的数据:

word:filename:wordCount
我想创建一个矩阵,其值如下:

the length of the matrix is same the number of the words in the file
the width is the number of the files.
例如:

apple:file1:2
apple:file3:4
cat:file1:3
tea:file2:5
ugly:file4:3
长度=5 宽度=4

我想要这样的输出:

apple:[2,0,4,0]
cat:[3,0,0,0]
tea:[0,5,0,0]
ugle:[0,0,0,3]
我尝试读取文本文件,然后将行拆分为“:”

然后我创建一个二维数组,如下所示:

String s[][]=new String[4][4];//the 4=the # of words, 4=number of files
我添加了4,因为我不知道如何从文件中获取多少文件

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
        String h=keys[2];//the word count
        s[i][j]=h;
    }
}
System.out.println(Arrays.deepToString(s))

请提供任何帮助:)

从您首选的输出来看,您似乎正在尝试构建整数数组的映射

这是我要做的

package q42705914;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Counter {
    private static final int MAX_FILES = 4;
    private final Map<String, int[]> counts = new HashMap<>();

    public Counter() {
        countAll("rawCounts.txt");
        print();
    }

    private void countAll(String filename) {
        try(FileReader fileReader = new FileReader(filename);
        BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                countLine(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void countLine(String line) {
        String[] parts = line.split(":");

        if(!counts.containsKey(parts[0])) {
            counts.put(parts[0], new int[MAX_FILES]);
        }
        int[] count = counts.get(parts[0]);

        int filenumber = Integer.valueOf(parts[1].substring(4));

        count[filenumber-1] = count[filenumber-1] + Integer.valueOf(parts[2]); 
    }

    private void print() {
        for(Entry<String, int[]> e : counts.entrySet()) {
            System.out.println(e.getKey() + ":" + Arrays.toString(e.getValue()));
        }
    }

    public static void main(String[] args) throws IOException {
        new Counter();
    }
}
包装q42705914;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.array;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Map.Entry;
公共课柜台{
私有静态最终int MAX_文件=4;
私有最终映射计数=新HashMap();
公众柜位(){
countAll(“rawCounts.txt”);
打印();
}
私有void countAll(字符串文件名){
try(FileReader FileReader=newfilereader(filename);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器)){
弦线;
而((line=bufferedReader.readLine())!=null){
计数线;
}
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
私有void countLine(字符串行){
String[]parts=line.split(“:”);
如果(!counts.containsKey(零件[0])){
counts.put(部分[0],新的int[MAX_文件]);
}
int[]count=counts.get(parts[0]);
int filenumber=Integer.valueOf(部分[1]。子字符串(4));
count[filenumber-1]=count[filenumber-1]+整数.valueOf(parts[2]);
}
私人作废打印(){
对于(条目e:counts.entrySet()){
System.out.println(e.getKey()+“:”+Arrays.toString(e.getValue());
}
}
公共静态void main(字符串[]args)引发IOException{
新计数器();
}
}

我正在讨论的代码中几乎没有问题,如果这对您有帮助的话

  • 正如您所提到的,
    key.length=#of words
    ,在不阅读整个文件的情况下,您如何知道有多少唯一的单词?你的问题中没有提到这一点,这使你的问题变得模糊

  • 更重要的是,
    String[]keys=line1.split(“:”)
    应该为您提供
    key.length=3
    ,因为您的输入格式是
    word:filename:wordCount


  • 在内部for循环中,
    for(int j=0;jdo您知道输入文件中会有多少单词和文件吗?因为这将是为您的问题编写解决方案的关键。欢迎使用堆栈溢出!看起来您需要学习使用调试器。请自行解决一些问题。如果以后仍有问题,请随时回来h更多细节。仍然存在一个问题,即在迭代所有单词时,如何知道哪个单词与特定索引相关?例如,单词“apple”映射到索引0,单词“cat”映射到索引1等等。如果没有这样的映射,你就不能在矩阵中的特定位置存储字数,不是吗?Stack Overflow是一个问答网站,不是一个写作业的服务。请不要给用户理由相信其他的。谢谢。@mike非常感谢mike,我使用的代码经过一些修改,它是正确的这是对的,第一点的单词与key.length不一样,我把它从问题中删除了。第三点,我必须按3迭代,因为循环从零开始。
    
    [[2, 2, 2], [2, 2, 2], [2, 2, 2]]
    [[4, 4, 4], [4, 4, 4], [4, 4, 4]]
    [[3, 3, 3], [3, 3, 3], [3, 3, 3]]
    [[5, 5, 5], [5, 5, 5], [5, 5, 5]]
    [[3, 3, 3], [3, 3, 3], [3, 3, 3]]
    
    package q42705914;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class Counter {
        private static final int MAX_FILES = 4;
        private final Map<String, int[]> counts = new HashMap<>();
    
        public Counter() {
            countAll("rawCounts.txt");
            print();
        }
    
        private void countAll(String filename) {
            try(FileReader fileReader = new FileReader(filename);
            BufferedReader bufferedReader = new BufferedReader(fileReader)) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    countLine(line);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        private void countLine(String line) {
            String[] parts = line.split(":");
    
            if(!counts.containsKey(parts[0])) {
                counts.put(parts[0], new int[MAX_FILES]);
            }
            int[] count = counts.get(parts[0]);
    
            int filenumber = Integer.valueOf(parts[1].substring(4));
    
            count[filenumber-1] = count[filenumber-1] + Integer.valueOf(parts[2]); 
        }
    
        private void print() {
            for(Entry<String, int[]> e : counts.entrySet()) {
                System.out.println(e.getKey() + ":" + Arrays.toString(e.getValue()));
            }
        }
    
        public static void main(String[] args) throws IOException {
            new Counter();
        }
    }