-5

I'm working with Java! The part I marked with two "Ö" I get "no return statement" error and on the part I marked with "// <----ÄÄÄ" I get "unreachable statement" error. That part of the code is supposed to delete a directory/folder with files in it and it actually works while it is by itself. But I cant seem to get it to work with the rest of my code! Help would be much appreciated!

In general this is a program that checks the size of the directory, deletes it and its contents and creates a new directory with the same name.

$package delete.file.or.dir;

import java.io.*;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class DeleteFileOrDir{




public static void main (String [] args){


// The directory in question

    File file = new File("C://123");

     long fileSize = file.length();

 System.out.println("File size in bytes is: " + fileSize);
 System.out.println("File size in KB is : " + (double)fileSize/1024);
 System.out.println("File size in MB is :" + (double)fileSize/(1024*1024));


  //Bad yes no statement

 JOptionPane pane = new JOptionPane(
    "Y=Yes\nN=No");
Object[] options = new String[] { "Y", "N" };
pane.setOptions(options);
JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
dialog.show();
Object obj = pane.getValue(); 
int result = -1;
for (int k = 0; k < options.length; k++)
  if (options[k].equals(obj))
    result = k;
System.out.println("User's choice: " + result);



 //Delete Directory with files

    deleteDirectory(new File("C://123"));
}

    //The first line of text just below is the first problem Ö

static public boolean deleteDirectory(File path) {
if( path.exists() ) {
  File[] files = path.listFiles();
  for(int i=0; i<files.length; i++) {
     if(files[i].isDirectory()) {
       deleteDirectory(files[i]);
     }
     else {
       files[i].delete();
     }
  }
}
return( path.delete() );



    File dir= new File("C://123");           // This gives me "unreachable statament Ä"

    boolean isDirectoryCreated = dir.mkdir();

    if(isDirectoryCreated)
        System.out.println("Reset");
    else
        System.out.println("Error");

}

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

6

because you already have return statement executed before that code

return( path.delete() );
jmj
  • 237,923
  • 42
  • 401
  • 438
  • should i remove the return statement... idk what to do :/ – Nikola Nestorovic Nov 26 '12 at 22:27
  • depends on what you are trying to do – jmj Nov 26 '12 at 22:28
  • I want the program to run, thats all, I really dont know what to do :P – Nikola Nestorovic Nov 26 '12 at 22:29
  • by the code after return statement it tries to create a dir on C:\\ and see if it gets created or not, and it prints according message, you can work with that – jmj Nov 26 '12 at 22:31
  • It does get created, When I used a simple delete directory instead of "delete non empty directory" code it worked! But when I switched it to the actual code I need it stopped working! Sorry for being this bad lol, oh and it printed according to message! – Nikola Nestorovic Nov 26 '12 at 22:33
  • remove the part after return if you want to skip the test – jmj Nov 26 '12 at 22:35
  • It works when I remove the create folder part (the one after return) but now I cant create a new folder... – Nikola Nestorovic Nov 26 '12 at 22:40
  • then put return at the end :) – jmj Nov 26 '12 at 23:01
  • You will hate me for this, really, but I alredy did that! I get no errors but then the dir doesnt get created... – Nikola Nestorovic Nov 26 '12 at 23:06
  • Also thanks for the effort, but I understand if this became too bothersome! I'll also try other sites :) – Nikola Nestorovic Nov 26 '12 at 23:11
  • Its very basic thing, changing site won't help you, rather go through the conversation again and understand the issue – jmj Nov 26 '12 at 23:16
  • I get it, If I put the return statement there then the create dir part wont work, but when I put the return statement under the create dir then a dir doesn't get created... It may be simple but Im VERY new to this, It's my first program :P Except for hello world that is :D – Nikola Nestorovic Nov 26 '12 at 23:19