Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Islamic University of Gaza Faculty of Engineering
Department of Computer Engineering ECOM 2124: JAVA II Lab
Lab # 2
Regular Expressions
&
Command Line Arguments
Eng. Haneen El-Masry
February, 2014
JAVA II Lab Eng.Haneen
2
Objective
To be familiar with Regular Expressions and Command Line Arguments.
Regular Expression
A regular expression (regex) is a string that describes a pattern for matching a set of
strings. Regular expression is a powerful tool for string manipulations. You can use
regular expressions for matching, replacing, and splitting strings.
Regular Expression Syntax
A regular expression consists of literal characters and special symbols.
Example 1: Strings Matching
Example 1 shows matching strings using some frequently used syntax for regular
expressions.
JAVA II Lab Eng.Haneen
3
JAVA II Lab Eng.Haneen
4
The Output:
JAVA II Lab Eng.Haneen
5
Example 2:
Write a java program that prompts the user to enter his name that must consist of first
name and last name separated by space, such that the first and last name consists of at
most twenty five letters, then if the name satisfied its pattern, let the user to enter his
phone number that must be ten digits. If there is an error, notify the user.
The Output:
JAVA II Lab Eng.Haneen
6
Example 3: Strings Replacing & Splitting
The Output:
JAVA II Lab Eng.Haneen
7
Command-Line Arguments
The main method has parameter args of String[] type. The main method is just a regular
method. Furthermore, You can pass strings to the main method from the command line
when you run the program. Strings passed to the main program are stored in args. The
first string is represented by args[0], and args.length is the number of strings passed.
Note: The strings are separated by a space. A string that contains a space must be
enclosed in double quotes.
Example 4:
Write a program passes an unspecified number of integers as separate strings to the
main method and displays their total.
JAVA II Lab Eng.Haneen
8
To run a NetBeans program from the Command Line, Follow the following steps:
1- File >> Project Properties.
2- Choose Run category.
JAVA II Lab Eng.Haneen
9
3- In Main class, browse for the program you wanted to run >> select Main Class.
4- Press OK.
JAVA II Lab Eng.Haneen
10
5- Run >> Clean and Build Project.
6- Copy the run command from NetBeans' Output window.
7- Open Command Window and set the path to JDK bin directory path.
JAVA II Lab Eng.Haneen
11
8- Paste the run command that you copied, and follow it by main method
arguments >> Enter.
Here, main method
Arguments must be.
JAVA II Lab Eng.Haneen
12
Exercises
1- (Checking password) Some Websites impose certain rules for passwords. Write
a method that checks whether a string is a valid password. Suppose the
password rule is as follows:
A password must have at least eight characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays "Valid
Password" if the rule is followed or "Invalid Password" otherwise.
2- (Finding the number of uppercase letters in a string) Write a program that
passes a string to the main method and displays the number of uppercase
letters in a string.
BONUS
(Sorting characters in a string) Write a method that returns a sorted string using
the following header:
public static String sort (String s)
For example, sort("acb") returns abc.
Write a test program that prompts the user to enter a string and displays the
sorted string.
Hint: You can use StringBuilder class and its methods.
Best Wishes