Hello,

I was asked to write a program that starts with a JFileChooser, the user is asked to choose a file. If at anytime during any part of this program, the user hits "cancel" or "yes" when the dialog shows "do you want to continue?" the program will return back to what they were doing before. If they hit "no" the program will terminate.

So I wrote this code and I used alot of methods. I am unsure if I am doing this right but when I asked the TA for help, he said my code was messy and complicated even though I felt as though I was fairly close. The problems im having is passing the users answer. the way ive found to do this is to use what I have stored in variable "a" the problem is that whenever I do use a, the pop up box comes up when I do not want it to, I just want to use what option he selected. This code is very confusing and this is the first time im asking for help in something Computer Science related and I am honestly ashamed :/ If someone could please help me I would appreciate it, thank you.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class FileFinder {

	public static void main(String[] args) {

		/*
		 * JFileChooser chooser = new JFileChooser();
		 * chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int
		 * code = chooser.showOpenDialog(null); if (code ==
		 * JFileChooser.APPROVE_OPTION) { File filename =
		 * chooser.getSelectedFile();
		 * 
		 * 
		 * }
		 */

		fileChooser();

		cont();

		Stringfind();

	}

	public static void fileChooser() {

		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int code = chooser.showOpenDialog(null);
		if (code == JFileChooser.APPROVE_OPTION) {
			File filename = chooser.getSelectedFile();

			if (a == JFileChooser.CANCEL_OPTION) {
				cont();
			}

			if (a == JFileChooser.APPROVE_OPTION) {
				Stringfind();
			}
		}

	}

	public static void cont() {

		int a = JOptionPane.showConfirmDialog(null, "Do you want to continue?");

		/*
		 * while (a == JFileChooser.APPROVE_OPTION) {
		 * JOptionPane.showConfirmDialog(null, "Do you want to continue?");
		 * fileChooser(); }
		 * 
		 * if (a != JFileChooser.APPROVE_OPTION) { System.exit(0); }
		 */

		if (a == JFileChooser.APPROVE_OPTION) {
			while (a == JFileChooser.APPROVE_OPTION) {
				// JOptionPane.showConfirmDialog(null,
				// "Do you want to continue?");
				fileChooser();
			}
		} else if (a == JOptionPane.CANCEL_OPTION) {
			fileChooser();

		} else {
			System.exit(0);
		}

	}

	public static void cont2() {

		int a = JOptionPane.showConfirmDialog(null, "Do you want to continue?");

		/*
		 * while (a == JFileChooser.APPROVE_OPTION) {
		 * JOptionPane.showConfirmDialog(null, "Do you want to continue?");
		 * fileChooser(); }
		 * 
		 * if (a != JFileChooser.APPROVE_OPTION) { System.exit(0); }
		 */

		if (a == JFileChooser.APPROVE_OPTION) {
			while (a == JFileChooser.APPROVE_OPTION) {
				// JOptionPane.showConfirmDialog(null,
				// "Do you want to continue?");
				Stringfind();
			}
		} else if (a == JOptionPane.CANCEL_OPTION) {
			Stringfind();

		} else {
			System.exit(0);
		}

	}

	public static void cont3() {

		int a = JOptionPane.showConfirmDialog(null, "Do you want to continue?");

		/*
		 * while (a == JFileChooser.APPROVE_OPTION) {
		 * JOptionPane.showConfirmDialog(null, "Do you want to continue?");
		 * fileChooser(); }
		 * 
		 * if (a != JFileChooser.APPROVE_OPTION) { System.exit(0); }
		 */

		if (a == JFileChooser.APPROVE_OPTION) {
			while (a == JFileChooser.APPROVE_OPTION) {
				// JOptionPane.showConfirmDialog(null,
				// "Do you want to continue?");
				tree(f, s);
			}
		} else if (a == JOptionPane.CANCEL_OPTION) {
			tree(f, s);

		} else {
			System.exit(0);
		}

	}

	public static void Stringfind() {

		String s = JOptionPane.showInputDialog("Enter the filename");
		if (a == JOptionPane.CANCEL_OPTION) {

			cont2();
		}

	}

	public static void FileFinder(File f, String s) { // is this going to redo the
												// first file again?
		if (f.isFile() ) {
			
			f.getName().contains(s);
			long bytes = 0;
			bytes = f.length();

			System.out.println("This file contains" + bytes + "bytes");
		} else {
			File[] trees = f.listFiles();
			for (int i = 0; i < trees.length; i++) {
								
				FileFinder(trees[i], s);

			}

		}

	}

	/*
	 * public static void asdf () {
	 * 
	 * int YES_NO_OPTION = 0;
	 * 
	 * JFileChooser chooser = new JFileChooser();
	 * chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int
	 * code = chooser.showOpenDialog(null); if (code ==
	 * JFileChooser.APPROVE_OPTION) { File filename = chooser.getSelectedFile();
	 * }
	 * 
	 * else { int status = JOptionPane.showConfirmDialog(null,
	 * "Do you want to continue?", null, YES_NO_OPTION);
	 * 
	 * while (status != JFileChooser.APPROVE_OPTION); { fileChooser(); }
	 * 
	 * }
	 * 
	 * }
	 */

}

I'm not sure about your line 64 and 70. You are creating a recursive call there. If a user click "OK"? In line 54, you declare "a" as a JOptionPane.showConfirmDialog(null, "Do you want to continue?");, so what buttons do you have there? It looks to me that the way to implement the method is actually throwing back to the loop...

Anyway, don't feel bad about TA doesn't help. The TA is a "lazy" bum. I used to work as a TA and I would never turn back students when they ask me questions especially about their code. It is one way of practice for me.

Just has a thought about how to solve your problem. How about changing your cont() method to return a boolean? Return true if it is OK to continue; otherwise, return false. Then the caller will check whether the returned value is true. If it is, ignore and do nothing; otherwise, exit the program?

I worked on it a little more yesterday and I am happy with the result. The program works as intended except 2 problems, 1: I need to add an FileNotFound exception and I do not know how to implement it with the way my method is now and 2: It needs to iterate through all the files in which the file equals the string, but the way its setup now, it will repeat that same file over and over again. The way im thinking about going about this is with a while loop but I am not completely sure. Any help is appreciated.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class FileFinder {

	public static void main(String[] args) {

		/*
		 * JFileChooser chooser = new JFileChooser();
		 * chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int
		 * code = chooser.showOpenDialog(null); if (code ==
		 * JFileChooser.APPROVE_OPTION) { File filename =
		 * chooser.getSelectedFile();
		 * 
		 * 
		 * }
		 */
		
		
		fileChooser();

	}
	
	/**
	 * This program runs the filechooser and prompts the user for a file and
	 * move accordingly.
	 */
	public static void fileChooser() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int code = chooser.showOpenDialog(null);
		if (code == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			Stringfind(file);
		}
		else if (code == JFileChooser.CANCEL_OPTION) {
			cont();
		}

	}
	
	/**
	 * This program will rerun filechooser if the program
	 * is disturbed when a user is choosing a file, if not
	 * it runs accordingly
	 */

	public static void cont() {

		int a = JOptionPane.showConfirmDialog(null, "Do you want to continue?");

		/*
		 * while (a == JFileChooser.APPROVE_OPTION) {
		 * JOptionPane.showConfirmDialog(null, "Do you want to continue?");
		 * fileChooser(); }
		 * 
		 * if (a != JFileChooser.APPROVE_OPTION) { System.exit(0); }
		 */

		if (a == JFileChooser.APPROVE_OPTION) {
		//	while (a == JFileChooser.APPROVE_OPTION) {
				// JOptionPane.showConfirmDialog(null,
				// "Do you want to continue?");
				fileChooser();
			//}
		} else if (a == JOptionPane.CANCEL_OPTION) {
			fileChooser();

		} else {
			System.exit(0);
		}

	}

	/**
	 * This program asks for a continue if a cancel is done when you are entering
	 * a string.
	 * @param file
	 */
	public static void cont2(File file) {

		int a = JOptionPane.showConfirmDialog(null, "Do you want to continue?");

		/*
		 * while (a == JFileChooser.APPROVE_OPTION) {
		 * JOptionPane.showConfirmDialog(null, "Do you want to continue?");
		 * fileChooser(); }
		 * 
		 * if (a != JFileChooser.APPROVE_OPTION) { System.exit(0); }
		 */

		if (a == JFileChooser.APPROVE_OPTION) {
			while (a == JFileChooser.APPROVE_OPTION) {
				// JOptionPane.showConfirmDialog(null,
				// "Do you want to continue?");
				Stringfind(file);
			}
		} else if (a == JOptionPane.CANCEL_OPTION) {
			Stringfind(file);

		} else {
			System.exit(0);
		}

	}

	//IF the user wants to keep going after the first file is a match
	/**
	 * This program continues after the users file has been found and acts accordingly
	 * @param file
	 * @param s
	 */
	public static void cont3(File file, String s) {

		int a = JOptionPane.showConfirmDialog(null, "Do you want to continue?");

		/*
		 * while (a == JFileChooser.APPROVE_OPTION) {
		 * JOptionPane.showConfirmDialog(null, "Do you want to continue?");
		 * fileChooser(); }
		 * 
		 * if (a != JFileChooser.APPROVE_OPTION) { System.exit(0); }
		 */
		/*
		 * 		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int code = chooser.showOpenDialog(null);
		if (code == JFileChooser.APPROVE_OPTION) {
			File filename = chooser.getSelectedFile();
			JOptionPane.showMessageDialog(null, "File count = " + fileCount(filename));
		}
		 */
		
		
		if (a == JFileChooser.APPROVE_OPTION) {
			//while (a == JFileChooser.APPROVE_OPTION) {
				// JOptionPane.showConfirmDialog(null,
				// "Do you want to continue?");
				tree(file, s);
			//}
		} else if (a == JOptionPane.CANCEL_OPTION) {
			tree(file, s);

		} else {
			System.exit(0);
		}

	}
		/**
		 * This program asks for the string file and continues with the program accordingly depends
		 * on the users selection
		 * @param file
		 */
	public static void Stringfind(File file) { 

		String s = JOptionPane.showInputDialog("Enter the filename");
	
	
		if (s == null) {
			cont2(file);
		}
		else
		{
			tree(file, s);
		}

	}
		
	/**
	 * This program finds out if its a file or folder and iterates through it accordingly,
	 * listing all files in a folder or finding a files bytes and directory
	 * @param f
	 * @param s
	 */
	public static void tree(File f, String s) { //Make this iterate through the next file.
												//Write a FileNotfound exception if no file with the string name exists.
												
		if (f.isFile() ) {
			
			if(f.getName().contains(s))	
			{
				long bytes = 0;
				bytes = f.length();
				System.out.println("This file contains" + bytes + "bytes");
				System.out.println(f.getAbsolutePath());
				cont3(f, s);
				
			}

			
		} else {
			File[] trees = f.listFiles();
			for (int i = 0; i < trees.length; i++) {
								
				tree(trees[i], s);

			}

		}

	}

	/*
	 * public static void asdf () {
	 * 
	 * int YES_NO_OPTION = 0;
	 * 
	 * JFileChooser chooser = new JFileChooser();
	 * chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int
	 * code = chooser.showOpenDialog(null); if (code ==
	 * JFileChooser.APPROVE_OPTION) { File filename = chooser.getSelectedFile();
	 * }
	 * 
	 * else { int status = JOptionPane.showConfirmDialog(null,
	 * "Do you want to continue?", null, YES_NO_OPTION);
	 * 
	 * while (status != JFileChooser.APPROVE_OPTION); { fileChooser(); }
	 * 
	 * }
	 * 
	 * }
	 */

}

I am not sure that JFileChooser would ever throw FileNotFoundException according to its API doc http://download.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html. Also, the way you handle in your tree() method (line 180) would not permit FileNotFoundException anyway. The method is taking a File object. If the file is not found, you wouldn't be able to create a valid File object. And I believe that JFileChooser handle that for you already. What you can check is that if the File object is null...

For your 2nd problem, I am not sure how you iterate through the tree to pick up the selected file. Could you be more clear in this? For example, give us a sample step-by-step of what you do and the result that you are talking about.

Well the way its setup is that when a user enters a string, the tree will go through and find a file with that string name. Currently, if no string of that filename exists, it will terminate. Instead, I want it to throw back a filenotfound exception. Im thinking to set it up like this:

public static void Stringfind(File file) throws FileNotFound { 

		String s = JOptionPane.showInputDialog("Enter the filename");
              if (s == null) {
			cont2(file);
		}
		try {
                       else
		{
			tree(file, s);
                      }
                          catch (FileNotFound)
                               catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "File not found!");
                          cont3();
                             }


		}

Or something like that.

On the problem it asks to iterate through all of the files and folders in a directory. So lets say we have a folder called "asdf" and in that folder we have 27 files called a through z with a duplicate a. So if when it prompts the user to enter a string, he will enter "a" this will make him search the first a file and tell you its size and directory. It will then ask for you to continue and then it will search more until another file is found that has a which is our second file. So the problem here is that it will keep on selecting the first a file without ever moving throughout the folder. And im thinking about iterating through a for loop to and to iterate through all of the files until one similar to the string is found.

Hope thats a more clear definition.

That clear things up a lot.

If you use GUI and JFileChooser to find a file, you won't get FileNotFoundException. If you let user enter a String, you will need to create your own File object and your StringFile should take "String" instead of "File" object. Then attempt to create a File object. Though, you will get NullPointerException if it cannot find the file. Still, you can throw a new FileNotFoundException instead.

In order to loop through the directory & its sub directories, you need to redesign the way you implement your code. You need a list to hold on which directories you have visited so far and which you should keep going. You may implement it in recursive fashion and use either DFS or BFS algorithm.

I have implemented your program but without GUI. If you want to see it, you may ask for it. However, you will need quite modification in order to make it work with GUI as you expect.

This article has been dead for over six months. Start a new discussion instead.