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