Sunday, April 29, 2007

Simple Input and Output

We have also started on doing some simple input and output using Java, because a program is only useful if it can interact with users to get inputs to the program and produce output for the user to see.

In Java, there are 2 ways to perform input and output, namely through the command line and through GUI.

Command Line
  • To perform an output to the command prompt window, you would use System.out.print() or System.out.println().
  • To get an input from the user, you would use a Scanner class, as in the example program in Lab 1 Practice 4.
  • You use entry.nextInt() to get an integer value, entry.nextDouble() to get a double value, and entry.nextLine() to get a string.
Graphical User Interface
  • To perform an output to a GUI window, you would use the showMessageDialog() method in the JOptionPane class.
  • To get an input from the user, you would use the showInputDialog() method, as in the example program in Lab 1 Practice 5.
  • Do take note that getting input from the user in a GUI is a 2-step process. The input value is always captured as a String object and you need to convert it into the appropriate data types in the 2nd step.

No comments: