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