In: Computer Science
So I need to parse tweets with the String class and I have no idea what should I do for this lab and how to start my coding.
It will be nice if anyone guide me what should I do exactly and how I start this lab.
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 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. Can we do simple math here to figure out the starting position of our 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.
The trim() method removes leading and trailing white spaces (if any) from a String. Use the trim() method on each resultant String.
Hint: The trim() method returns a modified String, but does not alter the String object that calls it. Ensure you are (re-)assigning a String to the result of trim().
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. HINT: Use the escape sequence \t to include tabs as needed
For example, if input is
"#typ offer; #det free essential supplies 4 evacs pets.; #loc 2323 55th st, boulder; #lat 40.022; #lng -105.226;"
The output should be like this :
Type: OFFER
Detail: free essential supplies 4 evacs pets.
Location: 2323 55th st- boulder
Latitude: 40.022
Longitude: -105.226
/*****************************Tweet.java*************************/
import java.util.Scanner;
public class Tweet {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Tweet:
");
String tweet =
scan.nextLine();
//declare variable and store
String type =
tweet.split(";")[0];
String detail =
tweet.split(";")[1];
String location =
tweet.split(";")[2];
String latitude =
tweet.split(";")[3];
String longitude =
tweet.split(";")[4];
//set value
type = type.trim().substring(5,
type.length()).toUpperCase();
detail = detail.trim().substring(5,
detail.length()-1);
location =
location.trim().substring(5, location.length()-1);
latitude =
latitude.trim().substring(5,latitude.length()-1).replaceAll(",",
"-");
longitude =
longitude.trim().substring(5,longitude.length()-1).replaceAll(",",
"-");
//print value
System.out.println("Type: " +
type);
System.out.println("Detail: " +
detail);
System.out.println("Location: " +
location);
System.out.println("Latitude: " +
latitude);
System.out.println("Longitude: " +
longitude);
}
}
/***************************output*****************************/
Enter Tweet: #typ offer; #det free essential supplies 4 evacs
pets.; #loc 2323 55th st, boulder; #lat 40.022; #lng
-105.226;
Type: OFFER
Detail: free essential supplies 4 evacs pets.
Location: 2323 55th st, boulder
Latitude: 40.022
Longitude: -105.226
Please let me know if you have any doubt or modify the answer, Thanks :)