By the end of the lab, you should:
• understand how String constants and String objects are represented in Java;
• be able to declare, initialize, and assign variables of type String;
• perform string processing using the methods of the String class;
• concatenate strings (and other values) together using the ‘+’ operator.
The lab deals with material from Chapter 2 (specifically, the discussions of the String class). It assumes you know how define simple classes in Java, and that you can declare and assign values to variables.
Exercise – Parsing Tweets
The class ParseTheTweet will read in a single tweet from the keyboard, and so when writing your program, you will need to use the Scanner class in addition to the String class. As usual, almost everything you write will go in the main method of the ParseTheTweet class.
The tweets processed by the call all encode the following information using so-called “hashtags”: The report type (#typ); some further detail (#det); a location (#loc) such as a street address; and latitude (#lat) and longitude (#lng). The type indicates the meaning of the tweet (i.e., whether it is a request for help or reports factual information), and the report detail provides additional information. Each of the hashtags is followed by some value (such as an actual latitude or longitude value).
When writing your code, you can assume that all of the tweets to process have the following format:
- At the top of your source file, include a comment stating your Java class name, your name, the date, the program purpose, and containing the statement of academic honesty as you did in previous labs.
- Use the Scanner class (as discussed in lecture) to read in a tweet entered by the user and store it in a String variable named tweet. You only need one Scanner.
- You will be splitting up (parsing) the information in the tweet into the 5 different types of information specified by the hashtags (type, location, detail, latitude, & longitude). Declare variables to store each of these pieces of information. Be sure to give these variables the appropriate data types and meaningful names.
- Now you actually need to divide the information in the tweet into the separate substrings of information. Declare 2 variables (start and finish) to hold the indices of where each substring starts and finishes.
- Once you have numbers assigned to start and finish, we want to discard the #tag and extract only the value. We know that the ‘;’ is where the value finishes – but we need to find the index where the actual value begins. Hint: our start variable currently points to the index of the “#” – and we know that all hashtag identifiers have the format, hashtag, 3 letters, and a space. Try to figure out a simple arithmetic formula to calculate the starting position of value.
- Once we have the correct starting and ending positions for the value, we want to extract the substring at those positions and assign it to the appropriate variable declared in step 3.
- Remember that for substring() the starting index is inclusive, but the ending index is exclusive. Read through your textbook about substring to refresh yourself on examples
- The trim() method removes leading and trailing white spaces (if any) from a String. Use the trim() method on each resultant String.
- After extracting the value encoded by each hashtag, we discard that part of the tweet String we are finished with it and are ready to repeat these steps for the next hashtag. We can use the substring method to extract the substring of our tweet variable starting where the last hashtag finished ( Hint: we know it finishes at a semicolon – and we have that index stored in our finish variable, and we want to start right after that semicolon – Also, remember that if we pass the substring method only 1 value, it begins at that index and goes until the end of the String).
- The type values come from a very small set of values, and we want to make them all caps.To do this, use the toUpperCase method of the String class.
- We also want to ensure that the detail and location values are free of commas (which might pose an obstacle to further processing). Use the replace method of the String class to do this. Replace each comma with a single hyphen (-).
- Now use System.out and print or println statements to produce formatted output, as shown in the included examples.
本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！
E-mail: [email protected] 微信:itcsdx