如何在嵌套的if语句中访问/获取变量的值到主if语句之外

时间:2017-03-29 12:44:35

标签: java if-statement file-io

我正在开发这个代码示例。这里我在主if-else语句之外声明了两个变量(initialPath,lastPath)。在主If语句中,有一个嵌套的if-else语句。我已经在嵌套的if语句中初始化了变量(initialPath)。我需要在嵌套的if-else语句之外使用该值。我附上了我的代码段。如果有人可以帮助解决我的问题,我很高兴:)

    FileWriter writer;
    File initialPath=null;
    File lastPath=null;

    if (clicked == 1) {


        int sf = savefile.showSaveDialog(null);

        if (sf == JFileChooser.APPROVE_OPTION) {

            initialPath=savefile.getSelectedFile(); // in here the Variable values is initialized/assigned

            try {

                if (savefile.getFileFilter().equals(filter2)) {

                    String path = savefile.getSelectedFile() + ".java";
                    File file = new File(path);
                    writer = new FileWriter(file, false);
                    System.out.println(savefile.getFileFilter());
                    writer.write(jTextPane1.getText());

                    writer.close();
                } else if (savefile.getFileFilter().equals(filter)) {

                    System.out.println("2");
                    String path = savefile.getSelectedFile() + ".txt";
                    File file = new File(path);
                    writer = new FileWriter(file, false);

                    writer.write(jTextPane1.getText());

                    writer.close();


                } else {
                    System.out.println("No format");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (sf == JFileChooser.CANCEL_OPTION) {


            clicked = 0;

        }

    } else {


        try {

            lastPath=initialPath.getParentFile(); //but in here lastPath become NULL because initialPath is NULL... 

    //I need to get the value of initialPath to this ELSE statement

            if (savefile.getFileFilter().equals(filter2)) {

                String path = savefile.getSelectedFile() + ".java";
                File file = new File(path);
                writer = new FileWriter(file, false);
                System.out.println(savefile.getFileFilter());
                writer.write(jTextPane1.getText());

                writer.close();
            } else if (savefile.getFileFilter().equals(filter)) {

                String path = savefile.getSelectedFile() + ".txt";
                File file = new File(path);
                writer = new FileWriter(file, false);

                writer.write(jTextPane1.getText());

                writer.close();


            } else {
                System.out.println("No format");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:0)

您是否可以将initialPath=savefile.getSelectedFile();移到if声明之外?

initialPath=savefile.getSelectedFile();
if (sf == JFileChooser.APPROVE_OPTION) {
    ...
}
else {
    ...
}

答案 1 :(得分:0)

您可以在外面宣布它时使用initialPath。 “我需要在嵌套的if-else语句之外使用该值。”

在一个简短的例子中你正在做什么:

File file;
if(true){
    file = /*you initialize here*/;
} else {
    /*if false file is never initialized but you want to use it here,
      its in scope but null of course.*/
}

如果你在if和else块中都需要它,那么将文件初始化到外部,如果你不能在外面初始化它,你就不需要在else块中。存在逻辑错误。