Search code examples
javadropboxjoptionpane

Java help JOptionPane Madness


Really needing help on this have been trying for the past hour playing around and can't seem to get it. Have looked for the question online and getting a lot of solutions that are similar but not quite accomplishing the task would appreciate if someone could help me with this?

I am currently working on a dropBox API and am trying to create a JOptionPane that prompts the user to enter the code generated from dropBox API and the program to read the input and verify.. I have done the System.in with success but this is for a GUI so obviously not helpful.

System.out.println("Enter Your auth code in this prompt and hit enter and wait..");
String result = JOptionPane.showInputDialog(null, "Enter Code Here: ");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine();

// Want JOptionPane to function the same way this ^ would behave

String info = JOptionPane.showInputDialog(code +" test " );
if(code == null){
    System.exit(1);
    return;
}
code = code.trim();
// This will fail if the user enters an invalid authorization code.
DbxAuthFinish authFinish = webAuth.finish(code);
String accessToken = authFinish.accessToken;
DbxClient client = new DbxClient(config, accessToken);
System.out.println("Linked account: " + client.getAccountInfo().displayName);
JOptionPane.showMessageDialog(null, "Hello..."+
    client.getAccountInfo().displayName+
    "     And Welcome To Our Community!");

Solution

  • The String 'result' should hold the code they entered into the JOptionPane.

    EDIT #1:

    String result = JOptionPane.showInputDialog(null, "Enter your auth code here:"); //Prompt for the auth code.  
    //If they didn't enter anything into the JOptionPane then close the program with code 1.
    if (result.isEmpty()) {
        System.exit(1);
    }
    System.out.println(result.trim()); //For testing purposes print the trimmed auth code to console.
    // [Omitted Code] //
    

    Also, you don't need to call return after a System.exit(#) because the program will never get to that code anyway.