计算Java中的单词对

计算Java中的单词对,java,collections,count,Java,Collections,Count,我有一个编程作业,这是我们班上第一次用Java编写代码。我已经问过我的导师,但没有得到任何帮助 程序需要计算文件中的字对,并按如下方式显示: abc: hec, 1 // Add all words (other than "---") into inputWords while (sc.hasNext()) { word = sc.next(); if (!word.equals("---")) { inputWords.add(wor

我有一个编程作业,这是我们班上第一次用Java编写代码。我已经问过我的导师,但没有得到任何帮助

程序需要计算文件中的字对,并按如下方式显示:

abc:
   hec, 1
// Add all words (other than "---") into inputWords
while (sc.hasNext()) {  
    word = sc.next();       
    if (!word.equals("---")) {
        inputWords.add(word);
    }
}

// Now iterate over inputWords and process each word one-by-one
for (int i = 0; i < inputWords.size(); i++) {
这意味着文本文件中只有一次“abc”后跟“hec”。我必须使用java中的Collections框架。这是我到目前为止所拥有的

 import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ArrayList;

// By default, this code will get its input data from the Java standard input,
// java.lang.System.in. To allow input to come from a file instead, which can be
// useful when debugging your code, you can provide a file name as the first
// command line argument. When you do this, the input data will come from the
// named file instead. If the input file is in the project directory, you will
// not need to provide any path information.
//
// In BlueJ, specify the command line argument when you call main().
//
// In Eclipse, specify the command line argument in the project's "Run Configuration."

public class Assignment1
{
    // returns an InputStream that gets data from the named file
    private static InputStream getFileInputStream(String fileName)
    {
    InputStream inputStream;

    try {
        inputStream = new FileInputStream(new File(fileName));
    }
    catch (FileNotFoundException e) {       // no file with this name exists
        System.err.println(e.getMessage());
        inputStream = null;
    }
    return inputStream;
    }

    public static void main(String[] args)
    {
    // Create an input stream for reading the data.  The default is
    // System.in (which is the keyboard).  If there is an arg provided
    // on the command line then we'll use the file instead.

    InputStream in = System.in;
    if (args.length >= 1) {
        in = getFileInputStream(args[0]);

    }

    // Now that we know where the data is coming from we'll start processing.  
    // Notice that getFileInputStream could have generated an error and left "in"
    // as null.  We should check that here and avoid trying to process the stream
    // data if there was an error.

    if (in != null) {

        // Using a Scanner object to read one word at a time from the input stream.

        @SuppressWarnings("resource")
        Scanner sc = new Scanner(in);   
        String word;

        System.out.printf("CS261 - Assignment 1 - Matheus Konzen Iser%n%n");

        // Continue getting words until we reach the end of input 
        List<String> inputWords = new ArrayList<String>();
        Map<String, List<String>> result = new HashMap<String, List<String>>();

        while (sc.hasNext()) {  
        word = sc.next();       
        if (!word.equals("---")) {

            // do something with each word in the input
            // replace this line with your code (probably more than one line of code)

            inputWords.add(word);
        }

            for(int i = 0; i < inputWords.size() - 1; i++){

                // Create references to this word and next word:
                String thisWord = inputWords.get(i);
                String nextWord = inputWords.get(i+1);

                // If this word is not in the result Map yet,
                // then add it and create a new empy list for it.
                if(!result.containsKey(thisWord)){
                    result.put(thisWord, new ArrayList<String>());
                }

                // Add nextWord to the list of adjacent words to thisWord:
                result.get(thisWord).add(nextWord);
            }


            //OUTPUT
            for(Entry e : result.entrySet()){
                System.out.println(e.getKey() + ":");

                // Count the number of unique instances in the list:
                Map<String, Integer>count = new HashMap<String, Integer>();
                List<String>words = (List)e.getValue();
                for(String s : words){
                    if(!count.containsKey(s)){
                        count.put(s, 1);
                    }
                    else{
                        count.put(s, count.get(s) + 1);
                    }
                }

                // Print the occurances of following symbols:
                for(Entry f : count.entrySet()){
                    System.out.println("   " + f.getKey() + ", " + f.getValue());
                }
            }


        }
        System.out.printf("%nbye...%n");
    }
    }
}

有人对此有什么想法或建议吗

我觉得这部分令人困惑:

while (sc.hasNext()) {  
    word = sc.next();       
    if (!word.equals("---")) {
        // do something with each word in the input
        // replace this line with your code (probably more than one line of code)

        inputWords.add(word);
    }

    for(int i = 0; i < inputWords.size() - 1; i++){
while(sc.hasNext()){
word=sc.next();
如果(!word.equals(“--”){
//对输入中的每个单词做些什么
//用代码替换此行(可能不止一行代码)
输入单词。添加(单词);
}
对于(int i=0;i
我想你的意思可能更像这样:

abc:
   hec, 1
// Add all words (other than "---") into inputWords
while (sc.hasNext()) {  
    word = sc.next();       
    if (!word.equals("---")) {
        inputWords.add(word);
    }
}

// Now iterate over inputWords and process each word one-by-one
for (int i = 0; i < inputWords.size(); i++) {
//将所有单词(除“--”)添加到inputWords中
而(sc.hasNext()){
word=sc.next();
如果(!word.equals(“--”){
输入单词。添加(单词);
}
}
//现在迭代输入单词并逐个处理每个单词
for(int i=0;i
看起来您正试图首先将所有单词读入
inputWords
,然后对它们进行处理,而代码在添加的每个单词之后遍历列表


还要注意的是,您在
for
循环中的条件过于保守,因此您将错过最后一个单词。删除
-1
将为每个单词提供索引。

我发现这部分令人困惑:

while (sc.hasNext()) {  
    word = sc.next();       
    if (!word.equals("---")) {
        // do something with each word in the input
        // replace this line with your code (probably more than one line of code)

        inputWords.add(word);
    }

    for(int i = 0; i < inputWords.size() - 1; i++){
while(sc.hasNext()){
word=sc.next();
如果(!word.equals(“--”){
//对输入中的每个单词做些什么
//用代码替换此行(可能不止一行代码)
输入单词。添加(单词);
}
对于(int i=0;i
我想你的意思可能更像这样:

abc:
   hec, 1
// Add all words (other than "---") into inputWords
while (sc.hasNext()) {  
    word = sc.next();       
    if (!word.equals("---")) {
        inputWords.add(word);
    }
}

// Now iterate over inputWords and process each word one-by-one
for (int i = 0; i < inputWords.size(); i++) {
//将所有单词(除“--”)添加到inputWords中
而(sc.hasNext()){
word=sc.next();
如果(!word.equals(“--”){
输入单词。添加(单词);
}
}
//现在迭代输入单词并逐个处理每个单词
for(int i=0;i
看起来您正试图首先将所有单词读入
inputWords
,然后对它们进行处理,而代码在添加的每个单词之后遍历列表

还要注意,您在
for
循环中的条件过于保守,因此您将错过最后一个单词。删除
-1
将为每个单词提供索引