Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在这个Java程序中restart()必须在try()中?_Java_Bufferedreader_Restart_Bufferedwriter_Finally - Fatal编程技术网

为什么在这个Java程序中restart()必须在try()中?

为什么在这个Java程序中restart()必须在try()中?,java,bufferedreader,restart,bufferedwriter,finally,Java,Bufferedreader,Restart,Bufferedwriter,Finally,你能帮我借用一下你的智慧吗 我现在正在寻找[add word mode]中的restart()方法必须位于finally{}部分的原因, 但我还是不明白为什么。目前,这两种方法都可以编译,我可以将restart()放在try{}或finally{}中。问题是如果我在try{}中输入它,当我选择[look all words mode]时,这个程序不会显示我在[add word mode]中输入的单词。如果我把它放在Finally{}中,它工作得很好,但我也很担心,因为我知道Finally{}部分

你能帮我借用一下你的智慧吗

我现在正在寻找[add word mode]中的restart()方法必须位于finally{}部分的原因, 但我还是不明白为什么。目前,这两种方法都可以编译,我可以将restart()放在try{}或finally{}中。问题是如果我在try{}中输入它,当我选择[look all words mode]时,这个程序不会显示我在[add word mode]中输入的单词。如果我把它放在Finally{}中,它工作得很好,但我也很担心,因为我知道Finally{}部分只用于close()方法。。。但它工作得很好!为什么?

另一个小问题是,如果我在addword()的finally{}部分关闭BufferedReader(br),它将显示restart()和start()的消息。在显示它们之后,程序结束时不接受我的键盘输入。为什么?

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){}
}

}

你能简单地描述一下你的课程想要达到什么目的吗?这是关于英语词汇的课程。如果我在[add word mode]中输入单词/意思,我可以通过在[word search mode]中键入单词来搜索它的意思!