帮忙看看哪错了

//过了个年,学的忘了好多,准备从数据结构开始复习,复习了TreeMap时就写了个小程序,是一个简单的辞典,就是输出同//义词的

//给定辞典文件和键盘输入的单词,找出其近义词

package TreeMAP;

public class GUI {

	public GUI(ThesaurusDrivers td){}
	public void println(String str){
		System.out.println(str) ;
	}
}



*
 * 一个简单的辞典
 */
package TreeMAP;

import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Thesaurus {

	private TreeMap thesaurusMap ;
	
	public Thesaurus(){
		thesaurusMap = new TreeMap() ;
	}
	public void add(String line){
		LinkedList synonymList = new LinkedList() ;
		StringTokenizer st = new StringTokenizer(line) ;
		String word = st.nextToken() ;
		while(st.hasMoreTokens())
			synonymList.add(st.nextToken()) ;
		thesaurusMap.put(word, synonymList) ;
	}
	public LinkedList getSynonyms(String word){
		return (LinkedList)thesaurusMap.get(word) ;
	}
}


package TreeMAP;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class ThesaurusDrivers {

	Thesaurus thesaurus ;
	GUI gui ;
	boolean readingInFileName ;
	
	public ThesaurusDrivers(){
		final String IN_FILE_PROMPT = "Please enter the path for the input file." ;
		gui = new GUI(this) ;
		thesaurus = new Thesaurus() ;
		readingInFileName = true ;
	}
	
	public void processInput(String str){
		
		final String WORD_PROMPT = "\n\nPlease enter a word. The sentinel is " ;
		final String SENTINEL = "***" ;
		final String WORD_NOT_FOUND_MESSAGE ="That word does not appear in hte thesaurus." ;
		final String SYNONYM_MESSAGE = "The synonyms of that word are " ;
		final String CLOSE_WINDOW_MESSAGE = "\n\nThe execution of the project is complete. Please close " + "this window when you are ready." ;
		
		LinkedList synonymList ;
		if(readingInFileName){
			if(thesaurusWasConstructed(str)){
				readingInFileName = false ;
				gui.println(WORD_PROMPT + SENTINEL) ;
			}
		}
		else if(!str.equals(SENTINEL)){
			synonymList = (LinkedList)thesaurus.getSynonyms(str) ;
			if(synonymList == null)
				gui.println(WORD_NOT_FOUND_MESSAGE) ;
			else{
				gui.println(SYNONYM_MESSAGE + synonymList) ;
				gui.println(WORD_PROMPT + SENTINEL) ;
			}			
		}
		else{
			gui.println(CLOSE_WINDOW_MESSAGE) ;
		}
	}
	
	protected boolean thesaurusWasConstructed (String inFileName){
		
		final String NO_INPUT_FILE_FOUND_MESSAGE = "Error: there is no file with that path. \n\n" ;
		final String ERROR_MESSAGE = "Exception:" ;
		final String IN_FILE_PROMPT = "Please enter the path for the input file." ;
		
		BufferedReader inFile ;
		String line ;
		boolean success = false ;
		
		try{
			inFile = new BufferedReader(new FileReader(inFileName)) ;
			success = true ;
			while((line = inFile.readLine()) != null)
				thesaurus.add(line) ;
			inFile.close() ;
		}catch(FileNotFoundException e){
			gui.println(NO_INPUT_FILE_FOUND_MESSAGE + IN_FILE_PROMPT) ;
		}catch(IOException e){
			gui.println(ERROR_MESSAGE + e) ;
		}
		return success ;
	}
}


package TreeMAP;

public class Test_Thesaurus {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
 
	
		ThesaurusDrivers td = new ThesaurusDrivers() ;
		td.processInput("thesaurus.in") ;
	}

}

/***/
/***thesaurus.in:

close near confined 
confined cramped 
correct true 
cramped confined 
near close 
one singular unique 
singular one unique 
true correct 
unique singular one
***/



 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值