/************************************************** Emalia wants to collect eMail addresses in a list. She starts with a program that inputs eMail addresses and saves them in an array. Before adding the address to the array, it VALIDATES by checking that it contains @ and . as required. ***************************************************/ public class Emails { String[] addresses = new String[1000]; int count = 0; // the number of addresses in the array public Emails() { String email=""; do { email = input("Type an eMail address (ENTER to quit)"); if( isValid(email) ) { addToAddresses(email); printAllAddresses(); } else { output(email + " is not valid"); } } while(email.length() > 0); // type nothing to quit } boolean isValid(String email) // check whether email is valid { if(email.length() < 5) // shortest is a@b.c { return false; } int atsign = email.indexOf("@"); // find the @ sign if(atsign < 0 ) { return false; } // @ is missing String server = email.substring(atsign+1); // the rest after @ int dot = server.indexOf("."); // find . in server if( dot < 0) { return false; } // . is missing return true; // if it got this far, it's valid } void addToAddresses(String email) // pre-condition : COUNT contains number of items in the array // post-condition: COUNT is 1 bigger and addresses[count] == email { addresses[count] = email; count = count + 1; } void printAllAddresses() { for(int c=0; c < count; c = c+1) { System.out.println(addresses[c]); } System.out.println("============"); } public static void main(String[] args) { new Emails(); } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } public void output(String message) { javax.swing.JOptionPane.showMessageDialog(null,message); } } |
Type an eMail address
(ENTER to quit): [bill@gates.com] bill@gates.com ========== Type an eMail address (ENTER to quit): [judy@hotmail] judy@hotmail is not valid bill@gates.com ========== Type an eMail address (ENTER to quit): [contact@bbc.co.uk] bill@gates.com contact@bbc.co.uk ========== |
In previous examples, we saw arrays that store data in the program.
In those cases, all the data is written explicitly
inside the program. It was not possible to change the data when the
program was running. This is unrealistic.
In real data-processing situations, we don't write data directly inside
the program. Rather, the USER adds data
as they wish.
This program is a first try at collecting data from the user and storing
it inside an array. Since data is being
added to the array, we cannot use addresses.length to control a loop that searches or prints
the array.
We need an extra variable that
keeps track of how many data items are in the array - we use count
for this purpose.
So the loop that prints out the entire list is:
for(int c = 0; c < count; c = c+1)
{ println( addresses[c]; }
Each time a new address is input and added to the list, the program must
increase count by 1:
addresses[count] =
email;
count = count + 1;
The program inputs a new eMail address
and then adds it to the list. But before adding it to the list,
it checks that the address is valid
- that means it is written in the correct format. There is no point
adding
an address if it is obviously wrong. Here are the business
rules that are enforced for the addresses:
This is just a good start for validation. There are more
requirements - for example, the '@' sign cannot
be the first character in the address.