Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 7 String proCeSSing

This snippet prints the following:

255.245.188.123 is valid? True 255.245.188.273 is valid? False

The first string (ipStr1) is a valid IP address and the second string (ipStr2) is not a valid IP address. The regex specified by you successfully identified the valid IP address. Don’t be alarmed at the lengthy regex; it will be easy once you understand it.

Let’s begin with the start and end symbols—"\b" is a boundary marker, as you saw earlier. Now, let’s look at the first group, "((25[0–5]|2[0–4]\d|[01]?\d\d?)(\.))", which has two parts: the first part specifies the regex for a number less than 256 and second part specifies a dot. The first part of the first group has 3 subexpressions.

The first subexpression specifies that the number could be in the range between 250 and 255. The second subexpression specifies that the number could be 200 to 249, and the third subexpression specifies that the number could be 0 to 199. You want to repeat this first group three times, so you place "{3}" immediately after the first group. The second group is nothing but the first group without a dot.

String Formatting

So far we have discussed how to search or parse a String object. What if you would like to format a string in a predefined template? For example, let’s assume that you are computing the area of a circle and you want to print the computed area with only two fractional digits. If you try the usual System.out.println method, it will print a quite big float number. As another example, say you want to separate the digits of a big number with separator such as a comma to print it to the end user. In such cases, you can use the C-style printf() (print formatted) method that was introduced in Java 5.

The method printf() uses string-formatting flags to format strings. It is quite similar to the printf() function provided in the library of the C programming language. The printf() method is provided as part of the PrintStream class. Here is its signature:

PrintStream printf(String format, Object... args)

The first parameter of the printf() method is a format string. A format string may contain string literals and format specifiers. The actual arguments are passed as the second arguments (args parameter here). This method can throw IllegalFormatException if the passed format is not correct.

Format specifiers are the crux of the string formatting concepts. They define the placeholder for a specific data type and its format (such as alignment and width). The remaining parameters of the printf() method are the variables (or literals) that provide the actual data to fill in the placeholders in sequence of the format specifiers.

Format Specifiers

Let’s investigate the template of format specifiers in the printf() method:

%[flags][width][.precision]datatype_specifier

As you can see, each format specifier starts with % sign followed by flags, width, and precision information and ends with data type specifier. In this string, the flags, width, and precision information is optional while the % sign and data type specifiers are mandatory.

Flags are single-character symbols that specify characteristics such as alignment and filling character. For instance, flag “-” specifies left alignment, “^” specifies center alignment, and “0” pads the number with leading zeroes.

The width specifier indicates the minimum number of characters that will span in the final formatted string.

If the input data is shorter than the specified width, then it is padded with spaces by default. In case the input data is bigger than the specified width, the full data appears in the output string without trimming.

218

Chapter 7 String Processing

The precision field specifies the number of precision digits in output string. This optional field is particularly useful with floating point numbers.

Finally, the data type specifier indicates the type of expected input data. The field is a placeholder for the specified input data. Table 7-6 provides a list of commonly used data type specifiers.

Table 7-6.  Commonly Used Data Type Specifiers

Symbol Description

%b

Boolean

%c

Character

%d

Decimal integer (signed)

%e

Floating point number in scientific format

%f

Floating point numer in decimal format

%g

Floating point numer in decimal or scientific format (depending on the

 

value passed as argument)

%h

Hashcode of the passed argument

%n

Line separator (new line character)

%o

Integer formatted as an octal value

%s

String

%t

Date/time

%x

Integer formatted as an hexadecimal value

 

 

Let’s use the printf() method to format the output of the area() method of a Circle class. By default, the area() method calculates the area of the circle and prints using the System.out.println() method. However, you want to control the format of the output. More specifically, you want to print the area with only two precision points. Listing 7-14 shows how to achieve this.

Listing 7-14.  Circle.java

// This program shows the usage of formatting string in printf() method class Circle {

private int x, y, radius;

public Circle(int x, int y, int radius) { this.x = x;

this.y = y; this.radius = radius;

}

void area() {

double tempArea = Math.PI * radius * radius;

System.out.println("Cirle area using default formatting with println: " + tempArea); System.out.printf("Circle area using format specifier with printf: %.2f", tempArea);

}

public static void main(String[] str) { Circle circle = new Circle(10,10,5); circle.area();

}

}

219

Chapter 7 String Processing

This program produces following output:

Cirle area using default formatting with println: 78.53981633974483 Circle area using format specifier with printf: 78.54

Well, you can see that the first line contains the unformatted long output while the second line contains formatted output using formatting string in printf() method.

Let’s use more formatting options in an example. Suppose that you want to print a table of soccer players along with their names, played matches, scored goals, and goals per match information. However, there are a

few constraints:

You want to print the name of players to the left (left aligned).

You want to specify at least 15 characters for the name of the players.

You want to print each column at a distance of a tab-stop.

You want to specify only one precision point in goals per match info. Listing 7-15 shows how to implement this.

Listing 7-15.  FormattedTable.java

 

// This program demonstrates the use of format specifiers in printf

 

class FormattedTable {

 

static void line() {

 

System.out.

 

println("-----------------------------------------------------------------

");

}

 

static void printHeader() {

 

System.out.printf("%-15s \t %s \t %s \t %s \n",

 

"Player", "Matches", "Goals", "Goals per match");

 

 

}

static void printRow(String player, int matches, int goals) { System.out.printf("%-15s \t %5d \t\t %d \t\t %.1f \n",

player, matches, goals, ((float)goals/(float)matches));

}

public static void main(String[] str) { FormattedTable.line(); FormattedTable.printHeader(); FormattedTable.line(); FormattedTable.printRow("Demando", 100, 122); FormattedTable.printRow("Mushi", 80, 100); FormattedTable.printRow("Peale", 150, 180); FormattedTable.line();

}

}

220

Chapter 7 String Processing

This program produces following output:

-----------------------------------------------------------------------------------

Player Matches Goals Goals per match

-----------------------------------------------------------------------------------

Demando

100

122

1.2

Mushi

80

100

1.3

Peale

150

180

1.2

-----------------------------------------------------------------------------------

Let’s analyze the format string specified in the printRow() method. The first part of the format string is "%-15s". Here, the expression starts with %, which indicates the start of a format-specifier string. The next symbol is '-', which is used to make the string left aligned. The number "15" specifies the width of the string and finally data type specifier of "s" indicates the input data type as String. The next format specifier string is "%5d", which signifies it expects

an integer that will be displayed in the minimum 5 digits. The last format specifier string is "%.1f", which expects a floating point number that will be displayed with one precision digit. All format specifier strings are separated with one or more "\t"s (tab stops) to make space between the columns.

Points to Remember

Here are some points that might prove useful on your OCPJP exam:

If you do not specify any string formatting specifier, the printf() method will not print anything from the given arguments!

Flags such as "-", "^", or "0" make sense only when you specify width with the format specifier string.

You can also print the % character in a format string; however, you need to use an escape sequence for it. In format specifier strings, % is an escape character—which means you need to use %% to print a single %.

If you do not provide the intended input data type as expected by the format string, then you can get an IllegalFormatConversionException. For instance, if you provide a string instead of an expected integer in your printRow() method implementation, you will get following exception:

Exception in thread "main" java.util.IllegalFormatConversionException:

d != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion (Unknown Source)

If you want to form a string and use it later rather than just printing it using the printf() method, you can use a static method in the String class—format(). We have reimplemented the printRow() method used in the last example using the format() method, as shown:

void printRow(String player, int matches, int goals){

String str = String.format("%-15s \t %5d \t\t %d \t\t %.1f \n", player, matches, goals, ((float)goals/(float)matches));

System.out.print(str);

}

221

Chapter 7 String Processing

Question time! 

1.Consider this program, which intends to print the word delimited by the characters ‘*’ in its both ends:

public class SearchString {

public static void main(String[] args) {

String quote = "An *onion* a day keeps everyone away!"; // match the word delimited by *'s

int startDelimit = quote.indexOf('*'); int endDelimit = quote.lastIndexOf("*");

System.out.println(quote.substring(startDelimit, endDelimit));

}

}

This program will print one of the following options:

A.*onion

B.*onion*

C.onion

D.*onio

E.nion*

Answer: A. *onion

(The substring (beginIndex, endIndex) consists of characters beginning from the character at beginIndex till the character at the endIndex – 1.)

2.Predict the outcome of the following program:

public class ParseString1 {

public static void main(String[] s) {

String quote = "Never lend books-nobody ever returns them!"; String [] words = quote.split(" ", 2);

// split strings based on the delimiter " " (space) for (String word : words) {

System.out.println(word);

}

}

}

A.It will result in a compile-time error.

B.It will result in a runtime exception.

C.It will print the following output when executed Never

lend

222

Chapter 7 String Processing

D.It will print the following output when executed Never

lend books-nobody ever returns them!

Answer: D. It will print the following output when executed Never

Lend books-nobody ever returns them!

(The second parameter of the split() method specifies the total number of strings that the split() method needs to generate. In the last string (here, in the second string), the split() method puts the remaining part of the string.)

3.Listing 7-11 used the regex "\w+@\w+\.com" to match e-mail addresses in a string where the e-mail addresses end with a “.com” domain. Can you replace the regex with the following: "\w+@\w+.com"?

A.Yes, it will work perfectly and will only match valid e-mail addresses.

B.No, it will result in a compile-time error.

C.No, it will result in a runtime exception.

D.No, it will produce wrong matches and will match invalid e-mail addresses. Answer: D. No, it will produce wrong matches and will match invalid e-mail addresses.

(It will produce wrong matches. For instance, it will match to "abc@xyz!com" also, since "." works here as a single character wildcard, which could match any character.)

4.Which of the following regular expressions is the correct expression for matching a mobile number stored in following format: +YY-XXXXXXXXXX (YY is the country code, the rest of the number is a mobile number)?

A.“\+\d{2}-\d{10}”

B.“\b\+\d{2}-\d{10}\b”

C.“+\d{2}-\d{10}”

D.“\b+\d{2}-\d{10}\b”

Answer: A. “\+\d{2}-\d{10}”

(You need to provide a backslash as an escape character for “+”. Another important point is that you cannot use “\b” in starting and ending if the first or last character of the string is not a word character.)

5.You want to write a regex to match an e-mail address. The e-mail address should not start with a digit and should end with “.com”. Which one of the following regex will satisfy this requirement?

A.“\b\w+@\w+\.com\b”

B.“\b\D\w*@\w+\.com\b”

C.“\b\D\w+@\w+\.com\b”

D.None of the above

223

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]