存储100个字符串并搜索文件i / o java

时间:2013-12-07 02:28:06

标签: java arrays

在这个程序中,我正在尝试编写一个程序,从一组文本文件中读取前100个字符串,然后计算这些字符串在整个文件中出现的次数。好吧,我不断得到一个疯狂的输出,我早些时候问过这个问题,但是把它弄得一团糟。有一件事发生了变化,但现在我的输出为null = 0.为100次

我的输出:http://i.imgur.com/WVZJnTp.png

    package program6;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

 public class Program6 {

public static final String INPUT_FILE_NAME = "myths.txt";
public static final String INPUT_FILE_NAME2 = "pnp.txt";
public static final String INPUT_FILE_NAME3 = "tsawyer.txt";

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    Scanner in = null;
    Scanner fin = null;
    Scanner fin2 = null;
    Scanner fin3 = null;
    String[] character = new String[100];
    int[] counter = new int[100];


    try {
        fin = new Scanner(new File(INPUT_FILE_NAME));
    } catch (FileNotFoundException e) {
        System.err.println("Error opening the file " + INPUT_FILE_NAME);
        System.exit(1);
    }
    try {
        fin2 = new Scanner(new File(INPUT_FILE_NAME2));
    } catch (FileNotFoundException e) {
        System.err.println("Error opening the file " + INPUT_FILE_NAME2);
        System.exit(1);
    }
    try {
        fin3 = new Scanner(new File(INPUT_FILE_NAME3));
    } catch (FileNotFoundException e) {
        System.err.println("Error opening the file " + INPUT_FILE_NAME3);
        System.exit(1);
    }
    for (int i = 0; i < character.length; i++) {
    }
    System.out.println("Word:              Count:");
    for (int i = 0; i < 100; i++) {
        System.out.println(character[i] + "         " + counter[i]);
    }

    }
    }

2 个答案:

答案 0 :(得分:2)

简单地替换

System.out.println(character + "         " + counter);

通过

    System.out.println(character[i] + "         " + counter[i]);

答案 1 :(得分:0)

在这一行System.out.println(character + " " + counter);

应该是:

System.out.println(character[i] + "         " + counter[i]);