134 Part (Web design software) II: Writing Your Own Java Programs
134 Part II: Writing Your Own Java Programs Figure 6-6 shows the flow of control in the loop of Listing 6-4. With a doloop, the situation in the twoPlusTwoprogram (shown earlier) can never happen. Because the doloop carries out its first action without testing a condition, every doloop is guaranteed to perform at least one iteration. Delete evidence? Was the replyeither yor n? no yes Figure 6-6: Here we go Delete the file, or don’t loop, do delete the file (depending on the reply) loop. Reading a single character In Listing 5-3, the user types a word on the keyboard. The myScanner.next method grabs the word and places the word into a Stringvariable named password. Everything works nicely because a Stringvariable can store many characters at once and the nextmethod can read many characters at once. But in Listing 6-4, you re not interested in reading several characters. You expect the user to type one letter either y or n. So you don t create a Stringvariable to store the user s response. Instead, you create a char variable a variable that stores just one symbol at a time. The Java API doesn t have a nextCharmethod. So to read something suitable for storage in a charvariable, you have to improvise. In Listing 6-4, the improvisation looks like this: myScanner.findWithinHorizon( . , 0).charAt(0) You can use this code exactly as it appears in Listing 6-4 whenever you want to read a single character. A Stringvariable can contain many characters or just one character. But a Stringvariable that contains only one character isn t the same as a char variable. No matter what you put in a Stringvariable, Stringvariables and charvariables have to be treated differently.
With our Unix hosting accounts you have total control of your Web site content from anywhere in the world.For more information please follow link Unix Web Hosting.