Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application 23.2Fundamentals of String s 23.3Constructing the Screen Scraping Application 23.4Locating Substrings in String s 23.5Extracting Substrings from String s 23.6Other String Methods 23.7Wrap-Up Tutorial 23 – Screen Scraping Application Introducing String Processing

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Create and manipulate String objects. –Use properties and methods of class String. –Search for substrings within String s. –Extract substrings from String s. –Replace substrings within String s.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 23.1 Test Driving the Screen Scraping Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 23.1 Test Driving the Screen Scraping Application (Cont.) JTextArea containing HTML Figure 23.1 Running the completed Screen Scraping application.

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 23.1 Test Driving the Screen Scraping Application (Cont.) Figure 23.2 Displaying the HTML page in Internet Explorer.

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 23.1 Test Driving the Screen Scraping Application (Cont.) Figure 23.3 Selecting an item name from the JComboBox. JComboBox ’s drop-down list

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 23.1 Test Driving the Screen Scraping Application (Cont.) Figure 23.4 Searching for the item’s price. Extracted price (converted to dollars) Price located in HTML string (specified in euros) Conversion rate

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 23.2 Fundamentals of String s String s –A series of characters treated as a single unit Special characters –+, -, *, /, white space and others String literals (String constants) –Sequences of characters in double quotation marks length method of class String –Returns the length, or the number of characters, in a String charAt method of class String –Returns the character located at a specific index in a String

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 23.2 Fundamentals of String s (Cont.) if ( string1.charAt( 0 ) == string2.charAt( 0 ) ) { messageJLabel.setText( "The first characters of string1 and string2 are the same." ); } Immutable –Characters in String s can not be changed after the String s are created

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 23.3 Constructing the Screen Scraping Application When the application is run Display the HTML that contains the items’ prices in a JTextArea When the Search JButton is clicked Retrieve the item selected from the JComboBox Search the HTML for the item the user selected Extract the item’s price Convert the item’s price from euros to dollars Display the item’s price in a JTextField

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 23.3 Constructing the Screen Scraping Application (Cont.)

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 23.4 Locating Substrings in String s Figure 23.6 HTML containing price of auction items. Substring –Sequence of characters that make up part or all of a String

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 23.4 Locating Substrings in String s (Cont.) Figure 23.7 Locating the desired item’s name. Search for the selectedItem in the String htmlText Method indexOf –Locates the first occurrence of the specified item name in the String

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 23.4 Locating Substrings in String s (Cont.) Figure 23.8 Locating the desired item’s price. Locate the beginning of the price in htmlText Locate the end of the price in htmlText Figure 23.9 Locating the end of the item’s price.

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 23.4 Locating Substrings in String s (Cont.) Method lastIndexOf –Locates the last occurrence of a substring in a String

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 23.5 Extracting Substrings from String s Figure 23.11 Retrieving the desired price. Method substring –Returns a new String object that contains a copy of a specified part of an existing String object Set priceText to htmlText substring, convert priceText to double and store in price

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 23.5 Extracting Substrings from String s (Cont.) Figure 23.12 Converting prices from euros to dollars. Convert price from euros to dollars using conversion rate entered by user

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 23.5 Extracting Substrings from String s (Cont.) Figure 23.13 Displaying the price in the priceJTextField. Display price in dollars

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 23.5 Extracting Substrings from String s (Cont.) Figure 23.14 Running the completed Screen Scraping application.

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 23.6 Other String Methods

21  2004 Prentice Hall, Inc. All rights reserved. Outline 21 ScreenScraping.java (1 of 8) 1 // Tutorial 23: ScreenScraping.java 2 // Search a HTML code String for an item and display its price 3 // converted from euros to American dollars. 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.text.*; 7 import javax.swing.*; 8 9 public class ScreenScraping extends JFrame 10 { 11 // JLabel and JComboBox for item names 12 private JLabel itemJLabel; 13 private JComboBox itemJComboBox; 14 15 // JLabel and JTextField for conversion rate 16 private JLabel rateJLabel; 17 private JTextField rateJTextField; 18 19 // JLabel and JTextField for item price 20 private JLabel priceJLabel; 21 private JTextField priceJTextField; 22 23 // JButton to search HTML code for item price 24 private JButton searchJButton; 25

22  2004 Prentice Hall, Inc. All rights reserved. Outline 22 ScreenScraping.java (2 of 8) 26 // JLabel and JTextArea for HTML code 27 private JLabel sourceJLabel; 28 private JTextArea sourceJTextArea; 29 30 // items that can be found in HTML code 31 private String[] items = { "Antique Rocking Chair", 32 "Silver Teapot", "Gold Pocket Watch" }; 33 34 // small piece of HTML code containing items and prices 35 private String htmlText = " " 36 + " Antique Rocking Chair " 37 + " €82.67 " 38 + " Silver Teapot " 39 + " €64.55 " 40 + " Gold Pocket Watch " 41 + " €128.83 " 42 + " "; 43 44 // no-argument constructor 45 public ScreenScraping() 46 { 47 createUserInterface(); 48 } 49 HTML provided in template describes three auction items and their prices in euros

23  2004 Prentice Hall, Inc. All rights reserved. Outline 23 ScreenScraping.java (3 of 8) 50 // create and position GUI components; register event handlers 51 private void createUserInterface() 52 { 53 // get content pane for attaching GUI components 54 Container contentPane = getContentPane(); 55 56 // enable explicit positioning of GUI components 57 contentPane.setLayout( null ); 58 59 // set up itemJLabel 60 itemJLabel = new JLabel(); 61 itemJLabel.setBounds( 8, 16, 40, 21 ); 62 itemJLabel.setText( "Item:" ); 63 contentPane.add( itemJLabel ); 64 65 // set up itemJComboBox 66 itemJComboBox = new JComboBox( items ); 67 itemJComboBox.setBounds( 56, 16, 184, 21 ); 68 contentPane.add( itemJComboBox ); 69 70 // set up rateJLabel 71 rateJLabel = new JLabel(); 72 rateJLabel.setBounds( 8, 48, 40, 21 ); 73 rateJLabel.setText( "Rate:" ); 74 contentPane.add( rateJLabel ); 75

24  2004 Prentice Hall, Inc. All rights reserved. Outline 24 ScreenScraping.java (4 of 8) 76 // set up rateJTextField 77 rateJTextField = new JTextField(); 78 rateJTextField.setBounds( 56, 48, 184, 21 ); 79 rateJTextField.setHorizontalAlignment( JTextField.RIGHT ); 80 contentPane.add( rateJTextField ); 81 82 // set up priceJLabel 83 priceJLabel = new JLabel(); 84 priceJLabel.setBounds( 8, 80, 40, 21 ); 85 priceJLabel.setText( "Price:" ); 86 contentPane.add( priceJLabel ); 87 88 // set up priceJTextField 89 priceJTextField = new JTextField(); 90 priceJTextField.setBounds( 56, 80, 96, 21 ); 91 priceJTextField.setHorizontalAlignment( JTextField.CENTER ); 92 priceJTextField.setEditable( false ); 93 contentPane.add( priceJTextField ); 94

25  2004 Prentice Hall, Inc. All rights reserved. Outline 25 ScreenScraping.java (5 of 8) 95 // set up searchJButton 96 searchJButton = new JButton(); 97 searchJButton.setBounds( 160, 80, 80, 23 ); 98 searchJButton.setText( "Search" ); 99 contentPane.add( searchJButton ); 100 searchJButton.addActionListener( 101 102 new ActionListener() // anonymous inner class 103 { 104 // event handler called when searchJButton is pressed 105 public void actionPerformed( ActionEvent event ) 106 { 107 searchJButtonActionPerformed( event ); 108 } 109 110 } // end anonymous inner class 111 112 ); // end call to addActionListener 113 114 // set up sourceJLabel 115 sourceJLabel = new JLabel(); 116 sourceJLabel.setBounds( 8, 112, 48, 16 ); 117 sourceJLabel.setText( "Source:" ); 118 contentPane.add( sourceJLabel ); 119

26  2004 Prentice Hall, Inc. All rights reserved. Outline 26 ScreenScraping.java (6 of 8) 120 // set up sourceJTextArea 121 sourceJTextArea = new JTextArea(); 122 sourceJTextArea.setBounds( 8, 136, 232, 105 ); 123 sourceJTextArea.setText( htmlText ); 124 sourceJTextArea.setLineWrap( true ); 125 contentPane.add( sourceJTextArea ); 126 127 // set properties of application's window 128 setTitle( "Screen Scraping" ); // set title bar text 129 setSize( 259, 278 ); // set window size 130 setVisible( true ); // display window 131 132 } // end method createUserInterface 133 134 // find and display price substring 135 private void searchJButtonActionPerformed( ActionEvent event ) 136 { 137 // get rate 138 String rate = rateJTextField.getText(); 139

27  2004 Prentice Hall, Inc. All rights reserved. Outline 27 ScreenScraping.java (7 of 8) 140 // rate is an empty string 141 if ( rate.equals( " " ) ) 142 { 143 JOptionPane.showMessageDialog( null, 144 “Please enter conversion rate first.", 145 "Missing Rate", JOptionPane.WARNING_MESSAGE ); 146 147 return; // exit method 148 } 149 150 // search for location of item and price 151 String selectedItem = 152 ( String ) itemJComboBox.getSelectedItem(); 153 int itemLocation = htmlText.indexOf( selectedItem ); 154 int priceBegin = htmlText.indexOf( "€", itemLocation ); 155 int priceEnd = htmlText.indexOf( " ", priceBegin ); 156 Retrieve the selected item and locate the indices where the price in euros begins and ends

28  2004 Prentice Hall, Inc. All rights reserved. Outline 28 ScreenScraping.java (8 of 8) 157 // store price found in double price 158 String priceText = htmlText.substring( 159 priceBegin + 6, priceEnd ); 160 double price = Double.parseDouble( priceText ); 161 162 // convert price from euros to dollars 163 double conversionRate = Double.parseDouble( 164 rateJTextField.getText() ); 165 price *= conversionRate; 166 167 // display price of item in priceJTextField 168 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 169 priceJTextField.setText( dollars.format( price ) ); 170 171 } // end method searchJButtonActionPerformed 172 173 // main method 174 public static void main( String args[] ) 175 { 176 ScreenScraping application = new ScreenScraping(); 177 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 178 179 } // end method main 180 181 } // end class ScreenScraping Extract price data from HTML string, excluding "€" Display price in dollars Convert price from euros to dollars using conversion rate entered by user


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application."

Similar presentations


Ads by Google