Java编程的中间,编写代码从文件已经读取了字典,然后字典添加内容

Java编程的中间,编写代码从文件已经读取了字典,然后字典添加内容,内容是重复的,怎么都把它们添加到字典里呢

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7785595
  • 你也可以参考下这篇文章:【Java】输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分
  • 你还可以看下java参考手册中的 java-学习Java语言 - 描述Java编程语言的基本概念和特点的课程。-泛型是Java编程语言的一个强大功能。它们提高了代码的类型安全性,使更多的错误可以在编译时发现。-通用方法
  • 除此之外, 这篇博客: java识别文件的编码方式中的 最近在整文件生成的功能,但是导出文件时老是编码错误,文件格式异常,所以就想有没有方法能够判断出某个文件的编码方式,后面就整了一个java代码去识别对应的文件编码格式。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    记得导包 ,所需要的包 : hsweb-utils-3.0.0.jar commons-io-2.5.jar

    package hello2;
    import java.io.File;
    import java.io.IOException;
    
    import org.apache.commons.io.FileUtils;
    import org.hswebframework.utils.file.EncodingDetect;      
    public class t4 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		String filePath="C:\\Users\\asus\\Desktop\\cs2.doc";  //需要判断的文件的路径    
    		//获得文件编码    
    		String fileEncode=EncodingDetect.getJavaEncode(filePath);     
    		System.out.println("文件编码格式:" + fileEncode);   //输出判断的文件编码格式
    		//根据文件编码获得文件内容    
    		try {
    			String fileContent=FileUtils.readFileToString(new File(filePath),fileEncode);  //这个一段可以不用
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    }
    
    

    jar包下载 :

    commons-io-2.5.jar 提取码:948z
    hsweb-utils-3.0.0.jar
  • 您还可以看一下 汪翠老师的java项目实战之欢乐斗地主游戏开发教程 毕业项目课程设计带源码课程中的 接收从服务器端群发回来的消息并添加地主牌小节, 巩固相关知识点
  • 以下回答来自chatgpt:

    Problem: Question Title: How to repeatedly add the contents of a previously read dictionary file to a dictionary in Java programming?

    Question Content: I am writing Java code where I have already read a dictionary from a file. Now I want to repeatedly add these contents to a dictionary, regardless of whether they already exist in the dictionary. How can I achieve this functionality?

    Solution: To achieve the functionality of repeatedly adding the contents of a previously read dictionary file to a dictionary, you can follow these steps:

    Step 1: Read the dictionary file First, you need to read the contents of the dictionary file into a List or an array. Here's an example of reading the contents from a file using BufferedReader:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class DictionaryReader {
        public static List<String> readDictionaryFile(String filePath) throws IOException {
            List<String> dictionary = new ArrayList<>();
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
    
            String line;
            while ((line = reader.readLine()) != null) {
                dictionary.add(line);
            }
    
            reader.close();
    
            return dictionary;
        }
    }
    

    Step 2: Create or initialize a dictionary You can use a Map implementation, such as HashMap, to store the dictionary words as keys. The values can be set to some default values or left empty. Here's an example of creating a dictionary:

    import java.util.HashMap;
    import java.util.Map;
    
    public class Dictionary {
        public static void main(String[] args) {
            Map<String, Integer> dictionary = new HashMap<>();
    
            // Add some default values
            dictionary.put("apple", 0);
            dictionary.put("banana", 0);
    
            // Alternatively, you can leave the dictionary empty initially
    
            // Rest of the code here...
        }
    }
    

    Step 3: Add the contents of the dictionary file to the dictionary Iterate over the previously read dictionary file and add each word to the dictionary. Here's an example of adding the contents to the dictionary:

    import java.util.List;
    import java.util.Map;
    
    public class Dictionary {
        public static void main(String[] args) {
            Map<String, Integer> dictionary = new HashMap<>();
    
            // Add some default values
            dictionary.put("apple", 0);
            dictionary.put("banana", 0);
    
            // Alternatively, you can leave the dictionary empty initially
    
            // Read the dictionary file
            try {
                List<String> dictionaryFromFile = DictionaryReader.readDictionaryFile("dictionary.txt");
    
                // Add the contents of the dictionary file to the dictionary
                for (String word : dictionaryFromFile) {
                    // Check if the word already exists in the dictionary
                    if (!dictionary.containsKey(word)) {
                        // Add the word to the dictionary
                        dictionary.put(word, 0);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // Rest of the code here...
        }
    }
    

    By following these steps, you will be able to repeatedly add the contents of a previously read dictionary file to a dictionary in Java programming.


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

关键步骤是:

使用 Map 表示字典
读取文件,拆分单词,添加到 Map 中
从控制台读取单词,添加到 Map 中
Map 中键为单词,值为出现次数

Map<String, Integer> map = new HashMap<>();

// 读取文件,拆分为单词        
Scanner scanner = new Scanner(new File("dictionary.txt"));
while(scanner.hasNext()) {
   String word = scanner.next();      
   map.put(word, map.getOrDefault(word, 0) + 1);
}
scanner.close();

// 从控制台读取单词        
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
    String word = in.next(); 
    map.put(word, map.getOrDefault(word, 0) + 1);
}
in.close();