String Examples and Practice

Name and Password Check

The following asks for a user's name. 
It should accept any name where the first 3 letters are correct.
After that, the user must type the correct password EXACTLY.

public PasswordCheck()
{
String name = input("Type your user name");

String start = name.substring(0,3);

if(start.equals("Sup"))
{
output("You must be Superman.");
String pw = input("Now type the password:");
if(pw.equals("Kryptonite"))
{
output("Correct!");
}
else
{
output("No, you are a phony");
}
}
}

public String input(String prompt)
{ return javax.swing.JOptionPane.showInputDialog(null,prompt); }

public void output(String message)
{ javax.swing.JOptionPane.showMessageDialog(null,message); }



Now this needs some improvements.
  1. Add a for... loop so that the user only gets 3 tries, then they are rejected.
  2. Make the password check case insensitive (e.g. the capital K doesn't matter)
  3. Make it a bit more demanding. 
    -  they must type the correct number of letters (8 letters)
    -  the first 2 letters must be correct and the last 2 letters must be correct.

Time Check

The following code asks the user to type in the time for an appointment.
They MUST type it in the format  hh:mm , like 08:45 .er
This code checks whether it is in the correct format.
It is incomplete - fix it.

   String time =  input("Type the time as hh:mm");

char third = time.charAt(____);

if( third == _______ )
{
output("okay");
}
else
{
output("BAD format");
}


Add some more commands to check the following:
  1. The input must contain exactly 5 characters
  2. The first character must be 0, 1 or 2
  3. The 5th character must be a digit 0,1,2,... 8 or 9
  4. The 4th character must be 0,1,2,3,4 or 5
  5. The second character must be 0..9 if the first is 0 or 1,
    but must be 0,1,2, or 3 if the first character is 2.


Practice

(1) Create a program that does the following:

(2) Create a program that has a "secret" word.
      The user must guess the secret word.
      The user gets 5 tries to guess the word.
       Before each guess, they get a hint.
       The hints can be anything like the following:

(3) Email validation
     
The code below checks whether an email address is valid -
      
it checks whether there is an '@' sign and a dot '.'.

   public EmailCheck()
   {
       String email = input("eMail address");
       int atSign = email.indexOf('@');

       if(atSign < 0)
       {  output("No @ sign"); }
       else
       {
          String name = email.substring(0,atSign);
          output("name = " + name);
       }      

       int dot = email.indexOf('.');
       if( dot < 0)
       {  
          output(". dot is missing");
       }
       else
       {
          String topDomain = email.substring(dot+1);
          output("Top level domain = " + topDomain);
       }       

       if(dot > atSign && atSign >= 0)
       {
          String domain = email.substring(atSign+1,dot);
          output("Domain = " + domain);
       }
   }



    (a)  The last part does NOT correctly display the domain.  Fix it.

    (b)  Add a check to make sure that the dot '.' is AFTER the '@' sign.
           For example, the following is wrong:   george.com@zoo

    (c)  It is possible that the name part of the email address contains a dot '.' , like:
           
                st.lawence@vatican.com

          Then the .indexof command will find the first dot instead of the second.
          Fix the code so that it finds the LAST dot '.'  each time.


(4) Time Strings
       A time is not a traditional "number".  Actually, it contains 2 numbers -
       the hour part and the minutes part, separated by a colon ':' .
       The following code extracts the hour and minutes from a time.
       Then it figures out how many minutes are remaining until the next whole hour.

   String time = input("Time as HH:MM");  // e.g. 10:35
String hour = time.substring(0,2);
String mins = time.substring(3,5);

int m = Integer.parseInt(mins);

int remaining = 60-m ;

output(remaining + " more minutes left");


(5)  Converting Money

     
Write a method that converts one currency to another.  For example:

          "50 EU to US"  -->  50 x 1.4 -->  70 US

      Make this convert between 3 different currencies, maybe  EU and US and BP (British Pounds).
      It must convert EU to US,  US to EU, BP to US, US to BP, EU to BP and BP to EU.
      So it needs 6 different conversion factors.
      The program must figure out what the 2 currencies are.  Then it needs to extract the
      number at the beginning.  Then use  Double.parseDouble  to convert the to a double variable.
      Multiply by the appropriate conversion factor to produce a result.