DOC PREVIEW
FIU COP 2210 - Reading User Input

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Computer Programming I Instructor: Greg ShawCOP 2210 Reading User Input(Using the showInputDialog method of the JOptionPane class)This document shows how Java makes it easy to use GUI elements,in this case a dialog box that allows the user of a program toenter input.I. The showInputDialog Method The JOptionPane class has a static method calledshowInputDialog that displays an input dialog box andreturns a string entered by the user. Syntax: string-var = JOptionPane.showInputDialog(prompt) ;where string-var is a String object variable, and prompt is a string expression Execution:1. An input dialog box containing the prompt is displayed 2. The user types the input and clicks the OK button3. The user input is returned and stored in string-var For the JOptionPane class, use this import statement:import javax.swing.JOptionPane ; Note that the user input is always returned as a string.II. The parseInt and parseDouble Methods To get numeric input, you must explicitly convert the stringreturned by showInputDialog to int or double. The Integer class has a static method, parseInt that takes astring argument and returns the argument converted to anint. Similarly, the Double class has a static method,parseDouble that takes a string argument and returns theargument converted to a double. Syntax: int-var = Integer.parseInt(arg) ; ordouble-var = Double.parseDouble(arg) ;where arg is a string literal, variable, or expression Execution: The argument is converted to an int or double,respectively, and stored in the variable to the left Note that if the argument contains any characters thatare not valid for an int or double literal, respectively,then a NumberFormatException is thrownIII. System.exit(0)When using the JOptionPane class, the last statement in themain method must be:System.exit(0) ;showInputDialog starts a new thread to handle user input. Athread is a program unit that is executed independently ofother parts of the program. This thread does not terminatewhen all statements in main have been executed so your programwill not end unless you explicitly call the exit method. Theparameter 0 tells the operating system that the programexecution was successfulIV. An Overloaded showInputDialog Method (Optional) The JOptionPane class also contains an overloaded version ofshowInputDialog that takes four arguments:string-var = JOptionPane.showInputDialog (null, prompt, title, JOptionPane.QUESTION_MESSAGE) ; string-var is a String object variable null is a Java keyword prompt is a string that displays above the text box title is a string that displays in the Title Bar JOptionPane.QUESTION_MESSAGE causes a "?" icon toappear- As an example of the four-argument showInputDialog method,the statement:String input = JOptionPane.showInputDialog(null, "What is the amount due?", "\u00BFCuanto es la cuenta?", JOptionPane.QUESTION_MESSAGE) ;will display this input dialog:  For examples of the one-argument showInputDialog methodand the parseInt and parseDouble methods, seeInputTest.java and


View Full Document
Download Reading User Input
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Reading User Input and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Reading User Input 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?