📄 dicservice.java
字号:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class DicService {
private String[] myindex;
private String[] mydic;
public DicService(){
indexDicserv();
}
//store the dictionary from files into array
public void indexDicserv(){
Scanner inputStream = null;
StringBuffer contentIndex = new StringBuffer();
try {
inputStream = new Scanner(new FileInputStream("myindex.txt"));
} catch (FileNotFoundException e) {
System.out.println("Error opening the file");
System.exit(0);
}
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
contentIndex.append(line+" ");
}
StringTokenizer tk = new StringTokenizer(contentIndex.toString());
myindex = new String[tk.countTokens()];
mydic = new String[tk.countTokens()];
for(int i = 0; i < myindex.length/2; i++){
myindex[i] = tk.nextToken();
tk.nextToken();
}
try {
inputStream = new Scanner(new FileInputStream("mydic.txt"));
} catch (FileNotFoundException e) {
System.out.println("Error opening the file");
System.exit(0);
}
int num = 0;
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
StringTokenizer tko= new StringTokenizer(line,"-");
tko.nextToken();
mydic[num] = tko.nextToken();
num++;
}
inputStream.close();
}
public String translate(String word){
System.out.print("word \"" + word + "\" to be searched");
int index = -1;
for(int i = 0; i < myindex.length/2; i++){
if(myindex[i].equalsIgnoreCase(word)){
index = i;
break;
}
}
System.out.println(index);
String description = "";
if(index == -1){
description = "Can not found in the dictionary!";
System.out.println(" Fail!");
}
else{
description = mydic[index];
System.out.println(" Success!");
}
return description;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -