繁体   English   中英

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

[英]How can I read strings into an array of strings from a file?

因此,我尝试读取前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);
        }
    }
}

尝试这个 :

Map<String, Integer> record = new HashMap<String, Integer>();
    for(String temp: StringArray){
        if(record.containsKey(temp)){
            Integer num = record.get(temp) + 1;
            record.put(temp, num);
        }
        else{
            record.put(temp, 1);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM