Java 用dat文件填充arrayList
代码如下:Java 用dat文件填充arrayList,java,arrays,arraylist,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&
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;
}
}
我的问题是,我无法让任何arrayLists
读入相应的.dat文件,从我的POV来看,我的代码似乎应该这样做
更新
除了缺少“Will”(我删除了该行)之外的电流输出:
导入java.io.File;
导入java.io.IOException;
导入java.util.Random;
导入java.util.Scanner;
导入java.util.ArrayList;
导入java.util.Collections;
导入静态java.lang.System.*;
公共类MadLib{
私有ArrayList谓词=新ArrayList();
私有ArrayList名词=新ArrayList();
私有ArrayList形容词=新ArrayList();
公共静态void main(字符串参数[]){
MadLib a=新的MadLib();
System.out.println(a.toString());
}
公共MadLib(){
loadAllWords();
System.out.println(名词);
}
公共MadLib(字符串文件名){
loadAllWords();
试一试{
扫描仪文件=新扫描仪(新文件(文件名));
}捕获(例外e){
e、 printStackTrace();
}
}
public void loadAllWords(){
加载名词();
加载动词();
形容词();
}
公共空负荷名词(){
nomes=loadFile(“nomes.dat”);
}
公共void loadVerbs(){
动词=加载文件(“verbs.dat”);
}
公共形容词(){
形容词=加载文件(“形容词.dat”);
}
公共ArrayList加载文件(字符串文件名){
ArrayList words=新的ArrayList();
试一试{
扫描仪切碎器=新扫描仪(新文件(文件名));
while(chopper.hasNext()){
words.add(chopper.next());
//只要调用nextLine,就会在文件末尾出现异常,除非您故意在那里有一个空行,所以这确保了它会出现异常
if(chopper.hasNext()){
chopper.nextLine();
}
}
chopper.close();
}捕获(例外e){
e、 printStackTrace();
}
返回单词;
}
公共字符串getRandomWord(ArrayList单词){
Random rand=新的Random();
int num=rand.nextInt(words.size());
字符串词=名词.get(num);
System.out.println(word);
返回词;
}
公共字符串getRandomVerb(){
if(动词==null){
加载名词();
}
返回单词(动词);
}
公共字符串getRandomNon(){
if(名词==null){
加载名词();
}
返回单词(名词);
}
公共字符串getRandom形容词(){
if(形容词==null){
加载名词();
}
返回单词(形容词);
}
公共字符串toString(){
在“+GetRandomNumber()+”+GetRandomNumber()+“+GetRandomNumber()+”+GetRandomNon()+”之后返回“+GetRandomNon()+”+getRandomVerb()+”,而“+GetRandomNon()+”+getRandomVerb()+”返回“+GetRandomNon()”;
}
}
您有任何输出吗?即使它的“意愿”?您应该在该printlnyes中尝试e.getMessage(),我确实得到了输出,但ArrayList似乎没有填充抛出的异常(如果有)?你到底得到了什么样的输出?这里也有很多重复的代码,只是在println()语句中使用了e.getMessage,它声称找不到指定的文件……那么该文件可能不在您假设的执行路径中。尝试修改文件绝对路径的文件名,或尝试计算相对路径。
dog
pig
chicken
building
car
person
place
thing
truck
city
state
school
student
bird
turkey
lion
tiger
alligator
elephant
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)
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();
}
}