Page 129 - Introduction to Programming with Java: A Problem Solving Approach
P. 129

                3.24 GUI Track: Input and Output with JOptionPane (Optional) 95
key’s newline character. Suppose that after the nextInt method call, you have a nextLine method call. The nextLine method call does not skip leading whitespace, so it’s stuck with reading whatever is left over from the previous input call. In our example, the previous input call left the enter key’s newline charac- ter. Thus, the nextLine call is stuck with reading it. Uh oh.
What happens if the nextLine method reads a newline character? It quits because it’s done reading a line (the newline character marks the end of a line, albeit a very short line). So the nextLine method call doesn’t get around to reading the next line, which is probably the line that the programmer intended it to read.
One solution to this nextLine problem is to include an extra nextLine method call whose sole purpose is to read in the leftover newline character. Another solution is to use one Scanner reference variable for nextLine input (e.g., stdIn1) and another Scanner reference variable for other input (e.g., stdIn2). But for the most part, we’ll try to steer clear of the problem altogether. We’ll try to avoid next- Line method calls that follow one of the other Scanner method calls.
As you progress through the book, you’ll see that input from the computer keyboard and output to the computer screen is all the I/O you need to solve a vast array of complex problems. But if you have a large amount of input, it might be easier and safer to use a simple text processor to write that input once into a file and then reread it from that file each time you rerun the program. And if you have a large amount of output, it might be easier to analyze the output if it’s stored in an output file. You don’t need to use files now, but if you can’t wait, you can find the details in Chapter 15, Sections 15.3 and 15.4. At this time you probably won’t be able to understand many of the details in those later sections of the book. But if you just consider the little program in Figure 15.2 to be a recipe, it will show you how to output to a file anything you can output to the computer screen. Likewise, if you just consider the little program in Figure 15.5 to be a recipe, it will show you how to input from a file anything you can input from the keyboard. Apago PDF Enhancer
At this point, some readers might want to apply what they’ve learned to an object-oriented programming (OOP) context. OOP is the idea that programs should be organized into objects. You’re not required to learn about OOP just yet, but if you can’t wait, you can find such details in Chapter 6, Sections 6.1 through 6.8.
3.24 GUI Track: Input and Output with JOptionPane (Optional)
This section is the second installment of our optional graphical user interface (GUI) track. In each GUI track section, we provide an introduction to a GUI concept. In this section, we describe how to implement rudimentary input/output (I/O) in a GUI window.
Up to this point in the chapter, we’ve used console windows for displaying input and output. In this sec- tion, we use GUI windows. What’s the difference? A console window is a window that can display text only. A GUI window is a window that can display not only text, but also graphical items like buttons, text boxes, and pictures. For an example, see Figure 3.16’s GUI window. It displays text (an installation message), a but- ton (an OK button), and a picture (a circled i icon).
Figure 3.16’s window is a specialized type of window. It’s called a dialog box. A dialog box performs just one specific task. Figure 3.16’s dialog box performs the task of displaying information (the i in the i icon stands for “information”). In later GUI track sections and again in Chapters 16 and 17, we’ll use general- purpose standard windows. But for now, we’ll stick with dialog boxes.
The JOptionPane Class and Its showMessageDialog Method
In order to display a dialog box, you need to use the JOptionPane class. The JOptionPane class is not part of the core Java language. So if you use the JOptionPane class, you need to tell the compiler where
 






















































































   127   128   129   130   131