Skip to content

Instantly share code, notes, and snippets.

@ucarion
Last active December 14, 2015 12:38
Show Gist options
  • Save ucarion/5087770 to your computer and use it in GitHub Desktop.
Save ucarion/5087770 to your computer and use it in GitHub Desktop.
JOptionPane hackery
public class JOptionPane {
/**
* input is essentially the same array as args
*/
private static String[] input;
/**
* ctr is the index of the value we want from args
* it starts at 0 and is incremented every time showInputDialog is called
*/
private static int ctr = 0;
/**
* returns the user's input string (the successive values of args)
* @param message the message that would be displayed if an input box were to be opened
* @return the "user"'s input string
* @precondition setArgs must have already been called
*/
public static String showInputDialog(String message) {
//increment the counter that determines which input value to return
ctr ++;
//if there's nothing in the input/args array, return an error message
if (input.length == 0)
return "first call the method setArgs";
//if the counter is larger than the number of input values, return the last input value
if (ctr > input.length)
return input[input.length - 1];
//return the appropriate input value for the number of times showMessageDialog has been called
return input[ctr - 1];
}
/**
* sets the class's input array instance variable to the array passed (args)
* @param args the array to get input values from
*/
public static void setArgs(String[] args) {
//set the input array to args
input = args;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment