简体   繁体   English

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

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

So I'm trying to read the first 100 strings, which are words into an array of 100 Strings. 因此,我尝试读取前100个字符串,这些单词是100个字符串数组中的单词。 and while doing that I'm trying to set each corresponding integer in an array of integers to 1, so counting each word the first time its read. 并且在这样做的同时,我尝试将整数数组中的每个对应整数设置为1,以便在首次读取每个单词时对其进行计数。

It's reading a book, 100 words at a time, and counting those words. 它正在读一本书,一次100个单词,然后计算这些单词。 So far I have this, how would I just make a switch statement of 100 cases? 到目前为止,我有100个案例的switch语句吗?

Thanks in advance for any help! 在此先感谢您的帮助!

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);
        }
    }
}

Try this : 尝试这个 :

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