用dat文件填充arrayList

时间:2013-01-10 23:19:02

标签: java arrays arraylist

以下是代码:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;

public class MadLib
{
private ArrayList<String> verbs = new ArrayList<String>();
private ArrayList<String> nouns = new ArrayList<String>();
private ArrayList<String> adjectives = new ArrayList<String>();

public MadLib()
{
    loadNouns();
    loadVerbs();
    loadAdjectives();
    out.println(nouns);
}

public MadLib(String fileName)
{
    //load stuff
    loadNouns();
    loadVerbs();
    loadAdjectives();


    try{
        Scanner file = new Scanner(new File(fileName));
    }
    catch(Exception e)
    {
        out.println("Houston we have a problem!");
    }
}

public void loadNouns()
{
    nouns = new ArrayList<String>();
    try{
        //nouns = new ArrayList<String>();
        String nou = "";
        Scanner chopper = new Scanner(new File ("nouns.dat"));

        //chopper.nextLine();
        while(chopper.hasNext()){
            nou = chopper.next();
            out.println(nou);
            nouns.add(nou);
            //chopper.nextLine();
        }
        //chopper.close();
        out.println(nouns.size());
    }
    catch(Exception e)
    {
        out.println("Will");
    }       
}

public void loadVerbs()
{
    verbs = new ArrayList<String>();
    try{
        Scanner chopper = new Scanner(new File("verbs.dat"));
        while(chopper.hasNext()){
            verbs.add(chopper.next());
            chopper.nextLine();
        }
        chopper.close();
    }
    catch(Exception e)
    {
        out.println("run");
    }
}

public void loadAdjectives()
{
    adjectives = new ArrayList<String>();
    try{
        Scanner chopper = new Scanner(new File("adjectives.dat"));
        while(chopper.hasNext()){
            adjectives.add(chopper.next());
            chopper.nextLine();
        }
        chopper.close();
    }
    catch(Exception e)
    {
    }
}

public String getRandomVerb()
{

    String verb = "";
    int num = 0;
    num = (int)(Math.random()*(verbs.size()-1));
    verb = verbs.get(num);
    return verb;
}

public String getRandomNoun()
{

    String noun = "";
    int num = 0;
    if(nouns == null){
        loadNouns();
    }
    double rand = (Math.random());
    num = (int)(rand * (nouns.size()-1));
    out.println(num);
    noun = nouns.get((int) num);
    out.print(noun);
    return noun;
}

public String getRandomAdjective()
{

    String adj = "";
    int num = 0;
    num = (int)(Math.random()*(adjectives.size()-1));
    adj = adjectives.get(num);
    return adj;
}       

public String toString()
{
    String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
   return output;
}
}

这里是其中一个.dat文件(除了它们包含的特定单词外,其他2个完全相同):

dog
pig
chicken
building
car
person
place
thing
truck
city
state
school
student
bird
turkey
lion
tiger
alligator
elephant

我的问题是,我无法让任何arrayLists读取相应的.dat文件和我的POV,我的代码似乎应该这样做

更新

除了没有“Will”之外的当前输出(我删除了该行):

run
[ ] 
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
at java.util.ArrayList.rangeCheck(Unknown Source) 
at java.util.ArrayList.get(Unknown Source)
at MadLib.getRandomNoun(MadLib.java:130)
at MadLib.toString(MadLib.java:147) 
at java.lang.String.valueOf(Unknown Source) 
at java.io.PrintStream.println(Unknown Source)
at Lab16d.main(Lab16d.java:18) 

1 个答案:

答案 0 :(得分:1)

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

import static java.lang.System.*;

public class MadLib {
    private ArrayList<String> verbs = new ArrayList<String>();
    private ArrayList<String> nouns = new ArrayList<String>();
    private ArrayList<String> adjectives = new ArrayList<String>();

    public static void main(String args[]) {
        MadLib a = new MadLib();
        System.out.println(a.toString());
    }

    public MadLib() {
        loadAllWords();
        System.out.println(nouns);
    }

    public MadLib(String fileName) {
        loadAllWords();

        try {
            Scanner file = new Scanner(new File(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loadAllWords() {
        loadNouns();
        loadVerbs();
        loadAdjectives();
    }

    public void loadNouns() {
        nouns = loadFile("nouns.dat");
    }

    public void loadVerbs() {
        verbs = loadFile("verbs.dat");
    }

    public void loadAdjectives() {
        adjectives = loadFile("adjectives.dat");
    }

    public ArrayList<String> loadFile(String filename) {
        ArrayList<String> words = new ArrayList<String>();
        try {
            Scanner chopper = new Scanner(new File(filename));
            while (chopper.hasNext()) {
                words.add(chopper.next());
                // just calling nextLine will cause an exception at the end of the file unless you have an blank line there on purpose, so this makes sure it does
                if (chopper.hasNext()) {
                    chopper.nextLine();
                }
            }
            chopper.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return words;
    }

    public String getRandomWord(ArrayList<String> words) {
        Random rand = new Random();
        int num = rand.nextInt(words.size());
        String word = nouns.get(num);
        System.out.println(word);
        return word;
    }

    public String getRandomVerb() {
        if (verbs == null) {
            loadNouns();
        }
        return getRandomWord(verbs);
    }

    public String getRandomNoun() {
        if (nouns == null) {
            loadNouns();
        }
        return getRandomWord(nouns);
    }

    public String getRandomAdjective() {
        if (adjectives == null) {
            loadNouns();
        }
        return getRandomWord(adjectives);
    }

    public String toString() {
        return "The " + getRandomNoun() + " " + getRandomVerb() + " after the " + getRandomAdjective()  + " " +  getRandomAdjective()  + " " +  getRandomNoun() + " while the " + getRandomNoun()  + " " +  getRandomVerb() + " the " + getRandomNoun();
    }
}