使用ArrayList的明显错误,但遵循给定的语法(Stanford CS106A HangmanLexicon)

时间:2016-03-25 11:28:37

标签: java arrays arraylist

我差不多完成了一个Hangman赋值(独立完成),最后一个任务是从.txt文件创建一个ArrayList。我遵循给定的语法,但编译器不会有两个错误:

/*
 * File: HangmanLexicon.java
 * -------------------------
 * This file contains a stub implementation of the HangmanLexicon
 * class that you will reimplement for Part III of the assignment.
 */

import acm.util.*;
import java.io.*;
import java.util.*;


public class HangmanLexicon {

	//random number for getWord
	private RandomGenerator rgen = RandomGenerator.getInstance();
		
	//creates new arraylist
	ArrayList<String> wordList = new ArrayList<String>();

	
	//opens the text file
	//Reads lines from the file into an ArrayList

	try {
		BufferedReader rd = new BufferedReader(new FileReader("HangmanLexicon.txt"));	
		while(rd.readLine() != null) {
		String line = rd.readLine();
		wordList.add(line);
		}	
	} catch (IOException ex) {
		System.out.println("bad/no file");
	}
	
	
// Returns the number of words in the lexicon
	public int getWordCount() {
		int WordCount = wordList.size();
		return WordCount;
	}
	
/** Returns the word at the specified index. */

	public String getWord(int index) {
		int lexnum = rgen.nextInt(0, index);
		String word = wordList.get(lexnum);
		return word;
	};

}

错误在第19和37行:

19)Arraylist ....错误:“{”预期在“;”之后

37)public int getWordCount(){....错误:“;”预期

对这些有什么解决方法?

1 个答案:

答案 0 :(得分:3)

try-catch块需要位于方法或静态块中。