gpt4 book ai didi

java - 如何从文件中将字符串读入字符串数组?

转载 作者:行者123 更新时间:2023-11-30 04:07:29 25 4
gpt4 key购买 nike

所以我尝试读取前 100 个字符串,它们是包含 100 个字符串的数组中的单词。在执行此操作时,我尝试将整数数组中的每个相应整数设置为 1,以便在第一次读取时对每个单词进行计数。

就是读一本书,一次读 100 个单词,然后数这些单词。到目前为止我已经有了这个,我该如何制作 100 个案例的 switch 语句?

预先感谢您的帮助!

package program6;
import java.util.Scanner;

public class Program6 {
static Scanner keyboard = new Scanner(System.in);
static String input;
String[] StringArray = new String[100];
int[] IntArray = new int[100];
String filename = "myths.txt";
String stringnumber;

public static void main(String[] args) {
for (int i = 0; i < 100; i++) {

最佳答案

HashMap<String,Integer> map = new HashMap();
public void count(String file){
Scanner in = null;
try{
in = new Scanner(new File(file));
}catch(IOException ex){

}
String val = in.next();
for(String currentKey : map.keySet()){
if(map.containsKey(val)){
map.put(currentKey,map.get(currentKey)+1);
}else{
map.put(val,1);
}
}
}

关于java - 如何从文件中将字符串读入字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20391613/

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