Would you please borrow your wisdom a little bit for me?
I am now looking for the reason why restart() method in [add word mode] must be in finally{} section, but I still don't figure out why. For now, both ways can be compiled either I put restart() in try{} or finally{}. The thing is if I input it in try{}, this program doesn't show me the word that I input in [add word mode] when I choose [look all words mode]. If I put it in Finally{}, It works well but I am also worried because I was learned that finally{} section is only for close() method... But It works well! Why?
Another little question is, if i close BufferedReader(br) at finally{} section in addword(), It shows message of restart() and start(). After showing them, The program ends without accepting my keyboard input. Why?
import java.util.Scanner;
import java.io.*;
import java.io.IOException;
public class EngDic{
public static void main(String[] args) {
try{
Dictionary1 dic1 = new Dictionary1();
dic1.start();
}
catch(IOException e){}
}
}
class Dictionary1 {
FileWriter write_target= null;
FileReader read_target= null;
BufferedReader br = null;
BufferedWriter bw= null;
void start() throws IOException {
System.out.println("Welcome!");
System.out.println("What are you going to do?");
System.out.println("1.add word 2.search word 3. look all words 4.exit");
int sel = System.in.read()-48; //my input through keyboard will be set as sel.
System.in.skip(2);
switch (sel){
case 1:addword();
break;
case 2:searchword();
break;
case 3:lookall();
break;
case 4:System.exit(0);
break;
default:
System.out.println("no such number!");
System.exit(0);
}
}
void addword(){
try{
br = new BufferedReader(new InputStreamReader(System.in));
write_target = new FileWriter("voca.txt",true);
bw= new BufferedWriter(write_target);
System.out.println("[add word mode] has been selected. Please enter the words");
System.out.println("CTR+Z means the end of input!");
String line;
while ((line=br.readLine()) !=null){
System.out.println(line);
bw.write(line);
bw.newLine();
}
//restart();
}
catch(IOException e){
System.out.println("-----error reason ------");
e.printStackTrace();
System.out.println("-----error reason ------");
}
finally {
//if (br != null) try{br.close();} catch(IOException e){}
if (bw != null) try{bw.close();} catch(IOException e){}
restart();
}
}
void searchword(){
try{
System.out.println("[search word mode] has been selected. Please enter the words");
read_target= new FileReader("voca.txt");
br= new BufferedReader(read_target);
Scanner input = new Scanner(System.in);
String search_word =input.nextLine();
String line;
boolean flag = false;
while ((line=br.readLine()) !=null){
String[] searched = line.split("/");
if (searched[0].equals(search_word)){
System.out.println("the meaning of the word you typed is " + searched[1]);
flag = true;
}
}
if (!(flag)){
System.out.println("There's no such word!");
}
restart();
}
catch(IOException e){
System.out.println("-----error------");
e.printStackTrace();
System.out.println("-----error------");
}
finally {
//if (br != null) try{br.close();} catch(IOException e){}
if (bw !=null) try{bw.close();} catch(IOException e){}
}
}
/*look all words mode method */
void lookall() {
System.out.println("[look all words mode] has been selected. Please enter the words");
try{
read_target= new FileReader("voca.txt");
br= new BufferedReader(read_target);
String line;
while ((line=br.readLine()) !=null){
System.out.println(line);
}
restart();
}
catch(IOException e){
System.out.println("-----error------");
e.printStackTrace();
System.out.println("-----error------");
}
finally {
//if (br != null) try{br.close();} catch(IOException e){}
if (bw !=null) try{bw.close();} catch(IOException e){}
}
}
void restart(){
try{
System.out.println("----------restart--------------");
start();
}
catch (IOException e){}
}
}