Figure 5-6: Authenticating a user in Java
Figure 5-6: Authenticating a user Code 3 Of 9 Generation In Java Using Barcode drawer for Java Control to generate, create Code 3/9 image in Java applications. The code in Listing 5-5 does a good job with nested if statements, but it does a terrible job with real-world user authentication First of all, never show a password in plain view (without asterisks to masquerade the password) Second, don t handle passwords without encrypting them Third, don t tell the malicious user which of the two words (the username or the password) was entered incorrectly Fourth well I could go on and on The code in Listing 5-5 just isn t meant to illustrate good username/password practices Barcode Drawer In Java Using Barcode printer for Java Control to generate, create barcode image in Java applications. Choosing among Many Alternatives (Java switch Statements) Scanning Barcode In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. I m the first to admit that I hate making decisions If things go wrong, I would rather have the problem be someone else s fault Writing the previous sections (on making decisions with Java s if statement) knocked the stuffing right out of me That s why my mind boggles as I begin this section on choosing among many alternatives What a relief it is to have that confession out of the way! Code 3/9 Creator In C#.NET Using Barcode drawer for .NET Control to generate, create Code 39 image in Visual Studio .NET applications. 5: Controlling Program Flow with Decision-Making Statements
Code 39 Creation In .NET Using Barcode encoder for Visual Studio .NET Control to generate, create Code 3 of 9 image in .NET framework applications. Does username equal "bburd" Draw Code-39 In VB.NET Using Barcode drawer for .NET Control to generate, create ANSI/AIM Code 39 image in VS .NET applications. Unknown user
USS Code 128 Creator In Java Using Barcode creator for Java Control to generate, create Code 128C image in Java applications. Does password equal "swordfish" Print Barcode In Java Using Barcode maker for Java Control to generate, create bar code image in Java applications. no Figure 5-7: Don t try eating with this fork
Draw Barcode In Java Using Barcode drawer for Java Control to generate, create barcode image in Java applications. Incorrect password
UPC - 13 Creator In Java Using Barcode drawer for Java Control to generate, create European Article Number 13 image in Java applications. You're in
GTIN - 12 Creation In Java Using Barcode creation for Java Control to generate, create UPC Code image in Java applications. Your basic switch statement
Create UPC - E0 In Java Using Barcode generation for Java Control to generate, create UPC - E1 image in Java applications. Now, it s time to explore situations in which you have a decision with many branches Take, for instance, the popular campfire song Al s All Wet (For a review of the lyrics, see the sidebar) You re eager to write code that prints this song s lyrics Fortunately, you don t have to type all the words over and over again Instead, you can take advantage of the repetition in the lyrics A complete program to display the Al s All Wet lyrics won t come until 6 In the meantime, assume that you have a variable named verse The value of verse is 1, 2, 3, or 4, depending on which verse of Al s All Wet you re trying to print You could have a big, clumsy bunch of if statements that checks each possible verse number Generate Data Matrix In Visual Basic .NET Using Barcode printer for .NET framework Control to generate, create Data Matrix image in VS .NET applications. Part II: Writing Your Own Java Programs
Drawing Barcode In Visual C#.NET Using Barcode generation for .NET framework Control to generate, create bar code image in VS .NET applications. if (verse == 1) { outprintln( That s because he has no brain ); } if (verse == 2) { outprintln( That s because he is a pain ); } if (verse == 3) { outprintln( Cause this is the last refrain ); } But that approach seems wasteful Why not create a statement that checks the value of verse just once and then takes an action based on the value that it finds Fortunately, just such a statement exists It s called a switch statement Listing 5-6 has an example of a switch statement Printing Code 39 Extended In .NET Framework Using Barcode creator for Visual Studio .NET Control to generate, create USS Code 39 image in Visual Studio .NET applications. Listing 5-6: ANSI/AIM Code 128 Scanner In .NET Framework Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications. A switch Statement
Generate Bar Code In .NET Framework Using Barcode maker for .NET framework Control to generate, create barcode image in .NET framework applications. import static javalangSystemout; import javautilScanner; class JustSwitchIt { public static void main(String args[]) { Scanner myScanner = new Scanner(Systemin); outprint( Which verse ); int verse = myScannernextInt(); switch (verse) { case 1: outprintln( That s because he has no brain ); break; case 2: outprintln( That s because he is a pain ); break; case 3: outprintln( Cause this is the last refrain ); break; default: outprintln( No such verse Please try again ); break; } outprintln( Ohhhhhhhh ); } } USS Code 128 Encoder In .NET Using Barcode creator for .NET Control to generate, create Code 128B image in VS .NET applications. 5: Controlling Program Flow with Decision-Making Statements
Generating Barcode In C#.NET Using Barcode generation for VS .NET Control to generate, create barcode image in Visual Studio .NET applications. Figure 5-8 shows two runs of the program in Listing 5-6 (The overall idea behind the program is illustrated in Figure 5-9) First, the user types a number, like the number 2 Then, execution of the program reaches the top of the switch statement The computer checks the value of the verse variable When the computer determines that the verse variable s value is 2, the computer checks each case of the switch statement The value 2 doesn t match the topmost case, so the computer proceeds on to the middle of the three cases The value posted for the middle case (the number 2) matches the value of the verse variable, so the computer executes the statements that come immediately after case 2 These two statements are outprintln( That s because he is a pain ); break; The first of the two statements displays the line That s because he is a pain on the screen The second statement is called a break statement (What a surprise!) When the computer encounters a break statement, the computer jumps out of whatever switch statement it s in So, in Listing 5-6, the computer skips right past the case that would display Cause this is the last refrain In fact, the computer jumps out of the entire switch statement and goes straight to the statement just after the end of the switch statement The computer displays Ohhhhhhhh because that s what the statement after the switch statement tells the computer to do EAN / UCC - 14 Drawer In Visual Basic .NET Using Barcode printer for VS .NET Control to generate, create EAN / UCC - 14 image in .NET applications. Figure 5-8: Running the code of Listing 5-6 If the pesky user asks for verse 6, the computer responds by dropping past cases 1, 2, and 3 Instead, the computer does the default In the default, the computer displays No such verse Please try again, and then breaks out of the switch statement After the computer is out of the switch statement, the computer displays Ohhhhhhhh You don t really need to put a break at the very end of a switch statement In Listing 5-6, the last break (the break that s part of the default) is just for the sake of overall tidiness
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |