Chapter 5: Controlling Program Flow with Decision-Making Statements (Space web hosting)
Chapter 5: Controlling Program Flow with Decision-Making Statements 111 string? To ask the second question (the more appropriate question) Java s Stringtype has a method named equals. (Like everything else in the known universe, this equalsmethod is defined in the Java API, short for Application Programming Interface.) The equalsmethod compares two strings to see whether they have the same characters in them. For an example using Java s equalsmethod, see Listing 5-3. (A run of the program in Listing 5-3 is shown in Figure 5-4.) Listing 5-3: Checking a Password import static java.lang.System.*; import java.util.Scanner; class CheckPassword { public static void main(String args[]) { out.print( What s the password? ); Scanner myScanner = new Scanner(in); String password = myScanner.next(); out.println( You typed >> + password + << ); out.println(); if (password == swordfish ) { out.println( The word you typed is stored ); out.println( in the same place as the real ); out.println( password. You must be a ); out.println( hacker. ); } else { out.println( The word you typed is not ); out.println( stored in the same place as ); out.println( the real password, but that s ); out.println( no big deal. ); } out.println(); if (password.equals( swordfish )) { out.println( The word you typed has the ); out.println( same characters as the real ); out.println( password. You can use our ); out.println( precious system. ); } else { out.println( The word you typed doesn t ); out.println( have the same characters as ); out.println( the real password. You can t ); out.println( use our precious system. ); } } }
We are comcast web hosting company willing to take you step further, please look comcat web hosting services.