import javax.swing.*; import uwcse.graphics.*; import java.io.*; import java.text.*; /** * Different options of standard I/O in java * */ public class InputOutput { /** * Examples of I/O */ public void doSomeIO() { // Input and output of an integer or a double int i; double x; String s; // use an Input object Input input = new Input(); i = input.readInt("Enter an integer (will recover if bad input)"); System.out.println("i="+i); // use a JOptionPane method s = JOptionPane.showInputDialog(null,"Enter an integer (crashes if bad input)", "Integer input",JOptionPane.QUESTION_MESSAGE); i = Integer.parseInt(s); JOptionPane.showMessageDialog(null,"i="+i,"Value of i", JOptionPane.INFORMATION_MESSAGE); // The complicated way (you don't need to understand this part) BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.print("Enter a double (crashes if bad input):"); s = r.readLine(); } catch(IOException ioe){/* ignore any the exception */} x = Double.parseDouble(s); DecimalFormat d = new DecimalFormat("0.000"); System.out.println("x="+d.format(x)); // Use a GWindow method GWindow window = new GWindow("Doing some I/O"); x=window.readDouble("Enter a dollar amount (recover if bad input)"); NumberFormat c = NumberFormat.getCurrencyInstance(); window.add(new TextShape("amount="+c.format(x),50,50)); } }