简体   繁体   中英

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. 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.

It's reading a book, 100 words at a time, and counting those words. So far I have this, how would I just make a switch statement of 100 cases?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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