package myIO; import javax.swing.*; public class IO { private static String outData = ""; private static int rows; private static int cols; public static int getInt(String prompt) { String num = JOptionPane.showInputDialog(prompt); try { int intNum = Integer.parseInt(num); // Remove comment below to echo input // appendOutput(prompt + "> " + intNum); return (intNum); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid integer " + num + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getInt(prompt); } } public static int getInt(String prompt, int min, int max) { String num = JOptionPane.showInputDialog(prompt); try { int intNum = Integer.parseInt(num); if (intNum >= min && intNum <= max) { return intNum; } else { JOptionPane.showMessageDialog(null, intNum + " is out of range " + min + " to " + max + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getInt(prompt, min, max); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid integer " + num + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getInt(prompt, min, max); } } public static double getDouble(String prompt) { String num = JOptionPane.showInputDialog(prompt); try { return (Double.parseDouble(num)); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid real number " + num + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getDouble(prompt); } } public static double getDouble(String prompt, int min, int max) { String num = JOptionPane.showInputDialog(prompt); try { double realNum = Double.parseDouble(num); if (realNum >= min && realNum <= max) return realNum; else { JOptionPane.showMessageDialog(null, realNum + " is out of range " + min + " to " + max + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getDouble(prompt, min, max); } } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid real number " + num + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getDouble(prompt, min, max); } } public static String getString(String prompt) { return JOptionPane.showInputDialog(prompt); } public static char getChar(String prompt) { String str = JOptionPane.showInputDialog(prompt); if (str.length() == 1) return str.charAt(0); else { JOptionPane.showMessageDialog(null, str + " is not a single character - try again", "Error", JOptionPane.ERROR_MESSAGE); return getChar(prompt); } } public static char getChar(String prompt, char low, char high) { String str = JOptionPane.showInputDialog(prompt); char tempCh = str.charAt(0); if (str.length() == 1 && tempCh >= low && tempCh <= high) return tempCh; else if (str.length() != 1) { JOptionPane.showMessageDialog(null, str + " is not a single character - try again", "Error", JOptionPane.ERROR_MESSAGE); return getChar(prompt, low, high); } else { JOptionPane.showMessageDialog(null, tempCh + " is out of range " + low + " to " + high + " - try again", "Error", JOptionPane.ERROR_MESSAGE); return getChar(prompt, low, high); } } public static boolean getBoolean(String query) { int n = JOptionPane.showConfirmDialog(null, query, "Yes/No question", JOptionPane.YES_NO_OPTION); return (n == 0); } public static boolean getBoolean(String query, String trueAns, String falseAns) { Object[] options = {trueAns, falseAns}; int n = JOptionPane.showOptionDialog(null, query, "Select " + trueAns + " or " + falseAns, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); return (n == 0); } public static int getChoice(String query, String[] options) { int n = JOptionPane.showOptionDialog(null, query, "Multiple choice question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); return n; } public static void displayResult(String message) { JOptionPane.showMessageDialog(null, message, "Program Results", JOptionPane.PLAIN_MESSAGE); } public static void displayResult(int i) { displayResult("" + i); } public static void displayResult(double d) { displayResult("" + d); } public static void displayResult(char c) { displayResult("" + c); } public static void displayResult(boolean b) { displayResult("" + b); } public static void appendOutput(String nextMessage) { outData += nextMessage + "\n"; //rows++; //cols = Math.max(cols, nextMessage.length()); } public static void appendOutput(int i) { appendOutput("" + i); } public static void appendOutput(double d) { appendOutput("" + d); } public static void appendOutput(char c) { appendOutput("" + c); } public static void appendResult(String nextMessage) { outData += nextMessage + "\n"; //rows++; //cols = Math.max(cols, nextMessage.length()); } public static void appendResult(int i) { appendOutput("" + i); } public static void appendResult(double d) { appendOutput("" + d); } public static void appendResult(char c) { appendOutput("" + c); } public static void display() { JTextArea outputArea; outputArea = new JTextArea(20, 40); outputArea.setText(outData); outputArea.setEditable(false); JScrollPane scrollOut = new JScrollPane(outputArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); JOptionPane.showMessageDialog(null, scrollOut, "Program Results", JOptionPane.INFORMATION_MESSAGE); } public static void displayOutput() { display(); } public static void clear() { outData = ""; } public static void clearOutput() { outData = ""; } }