0

This must be a pretty simple fix, I've been working on this for a couple hours now, as I'm new to Java. So, why isn't the program continuing after I select the my 'yes' option to continue? It just idles.. the browse file input for the keyfile with one of two passwords [beep11, or beep22] is supposed to pop up, the user then picks the file, and it gives the output. Here's the code.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class TxtKeyVerifier2 {

public static void main(String[] args) throws FileNotFoundException {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Do you to continue yes/no: ");
    String answer = keyboard.nextLine();

    switch (answer) {
        case "yes":
        case "y": 
        {
            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.showOpenDialog(null);
            File keyfile = fileChooser.getSelectedFile();

            Scanner sc = new Scanner(keyfile);
            String input = sc.nextLine();


            if (authenticate1(input)) {

                System.out.println("This program is working if this text is found within outputfile.txt.");

                File outputfile = new File("outputfile.txt");
                FileOutputStream fos = new FileOutputStream(outputfile);
                PrintStream ps = new PrintStream(fos);
                System.setOut(ps);
                System.out.println("This program is working if this text is found within outputfile.txt.");

            }else if (authenticate2(input)) {

                System.out.println("It works.");

            }else{
                System.out.println("Error: Wrong password.");
            }
        }
        break;
        case "no":
        case "n":
            break;

        default:
            System.out.println("Invalid choice");
    }
}

private static boolean authenticate1(String password1) {

    return ((password1.length() == 6)
            && (password1.matches("beep11"))
            && (password1.matches("beep11"))
            && (password1.matches("beep11")));
}

private static boolean authenticate2(String password2) {

    return ((password2.length() == 6)
            && (password2.matches("beep22"))
            && (password2.matches("beep22"))
            && (password2.matches("beep22")));
}
}
NumaNuma
  • 39
  • 4
  • Well, have you stepped through every line of code in a debugger? – OldProgrammer Dec 02 '16 at 02:59
  • why do you have multiple keyboard scanners? – Scary Wombat Dec 02 '16 at 03:01
  • I just ran the debugger, and ran the program. The browse files window still didn't come up. This must be something really simple. – NumaNuma Dec 02 '16 at 03:01
  • Hi again, Scary Wombat. Keyboard2 is for the yes/no verification, and I left the initial keyboard scanner there despite how I took it out before posting this and it did nothing. There's no errors that pop up in the code currently. But, as I said. I took the original keyboard scanner out, ran the program, and it didn't do anything. – NumaNuma Dec 02 '16 at 03:04
  • I took the second keyboard scanner out, since now people know I tried it without it previous to posting the question. Any help is appreciated. This is for my college final project. I want to go into cyber security. Thanks. – NumaNuma Dec 02 '16 at 03:18

1 Answers1

2

I went through your code and noticed that you have not used correct way of using JFileChooser in your code. I have added sample code to use the JFileChooser and go through the bellow code which you can get your expected output.

ChooseFile class will have the required code to open a popup of FileChooser to select the specific file you needed.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class TxtKeyVerifier2 {

public static void main(String[] args) throws FileNotFoundException {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Do you to continue yes/no: ");
    String answer = keyboard.nextLine();

    switch (answer) {
    case "yes":
    case "y": {
        ChooseFile fileChooser = new ChooseFile();
        File keyfile = fileChooser.getFile();

        Scanner sc = new Scanner(keyfile);
        String input = sc.nextLine();

        if (authenticate1(input)) {

            System.out.println("This program is working if this text is found within outputfile.txt.");

            File outputfile = new File("outputfile.txt");
            FileOutputStream fos = new FileOutputStream(outputfile);
            PrintStream ps = new PrintStream(fos);
            System.setOut(ps);
            System.out.println("This program is working if this text is found within outputfile.txt.");

        } else if (authenticate2(input)) {

            System.out.println("It works.");

        } else {
            System.out.println("Error: Wrong password.");
        }
    }
        break;
    case "no":
    case "n":
        break;

    default:
        System.out.println("Invalid choice");
    }
}

private static boolean authenticate1(String password1) {

    return ((password1.length() == 6) && (password1.matches("beep11")) && (password1.matches("beep11"))
            && (password1.matches("beep11")));
}

private static boolean authenticate2(String password2) {

    return ((password2.length() == 6) && (password2.matches("beep22")) && (password2.matches("beep22"))
            && (password2.matches("beep22")));
}
}

class ChooseFile {
private JFrame frame;

public ChooseFile() {

    frame = new JFrame();
    frame.toFront();
    frame.setVisible(true);

}

public File getFile() {
    JFileChooser fc = new JFileChooser();
    if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(null)) {
        frame.setVisible(false);
        return fc.getSelectedFile();
    } else {
        System.out.println("Next time select a file.");
        System.exit(1);
    }
    return null;
}

private void BringToFront() {
    frame.setExtendedState(JFrame.ICONIFIED);
    frame.setExtendedState(JFrame.NORMAL);

}

}
NumaNuma
  • 39
  • 4
Coder
  • 1,917
  • 3
  • 17
  • 33