mercredi 29 mars 2017

How to access/get the value of a Variable inside a nested if statement to outside the main if statement

I have this code sample which I was developing. Here I have declared two variables (initialPath, lastPath) outside the main if-else statement. Inside the main If statement there is a nested if-else statement. I have initialized the variable (initialPath) inside the nested if statement. I need that value to be used outside the nested if-else statement. I have attached my code snippet. If anyone can help to solve my problem, I'd be glad :)

    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();
        }

Aucun commentaire:

Enregistrer un commentaire