Question

In: Computer Science

So I need to parse tweets with the String class and I have no idea what...

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

Solutions

Expert Solution

/*****************************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 :)


Related Solutions

I need the solution by Hand since I have no idea About Excel and am not...
I need the solution by Hand since I have no idea About Excel and am not allowed to use it on the exam. TIA 1. What is the beta of firm XXX over a 6 month period ? Could you please explain very thoroughly and step-by-step. I really struggle with the topic yet Average month market return: 1/1/1/-1/-1/-1 Average stock Price changes: 2/0/1/-1/0/-2
Redo Program 1 so that you have a template class, that works with a char, string,...
Redo Program 1 so that you have a template class, that works with a char, string, int, or double data type. Program 1 is below: #include <iostream> #include <string> #define CAPACITY 12 using namespace std; class queue { private: string strArray[CAPACITY]; int frontIndex, endIndex; int queueSize; public: queue() { frontIndex = endIndex = -1; queueSize = 0; } void enqueue(string); string dequeue(); int size() { return queueSize; } int front() { return frontIndex; } int end() { return endIndex; }...
Hi, I need to distinguish between fixed costs and variable costs, but I have no idea...
Hi, I need to distinguish between fixed costs and variable costs, but I have no idea where to even start. Could you please explain? Simple answer will suffice. Thank you Revenue Total revenue Cost of revenue Gross profit Operating activities Research & development Selling, general & administrative Non-recurring Other operating expenses Total operating expenses Operating income Income from continuing operations Net other income EBIT Interest expense Income before tax Income tax expense Minority interest Net income from continuing ops Non-recurring...
What is ram (in computer desktop)? why do we need ram? so, If I have to...
What is ram (in computer desktop)? why do we need ram? so, If I have to analysis Ram 36 GB (8GB x4) DDR4/ 3000 MHZ. what is 36 GB of ram? what is DDR4? and what is 3000 MHZ?
I have GIS class. I want to do project, and I need to use ARCmap to...
I have GIS class. I want to do project, and I need to use ARCmap to search population of homeless at Denver Colorado. Can you help me to explain how to find the Population of homeless? Please
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I have this question in my nursing class I just need someone to answer it for...
I have this question in my nursing class I just need someone to answer it for me Reflect on an occasion where you experienced ineffective leadership (doesn't have to be in the hospital/healthcare). What behaviors did they display? What factors may have influenced their leadership style?
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square...
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square shade = new Square(getside()); System.out.println("Your chooses are:"); System.out.println("\nCircle"); System.out.println("Triangle"); System.out.println("Rectangle"); System.out.println("Square"); boolean moreShapes = true; boolean userChoice = true; while(moreShapes) { Scanner shapes = new Scanner (System.in); System.out.println("\nWhat shape do you want:"); String yourShape = shapes.nextLine(); if (userChoice = shade != null) { shade.getSide(); } System.out.println("\nWhat are two size paramters of the shape you choose:"); String yourParameter = shapes.nextLine(); System.out.println("\nYour shape is: " +...
How much money will I need to have at retirement so I can withdraw $60,000 a...
How much money will I need to have at retirement so I can withdraw $60,000 a year for 20 years from an account earning 8% compounded annually? a. How much do you need in your account at the beginning? b. How much total money will you pull out of the account? c. How much of that money is interest?
Hello I have these questions. I need good answers in order to undertand not so long...
Hello I have these questions. I need good answers in order to undertand not so long nor so short please Why are consumers willing to pay more for small portions? What is the substitution effect of a higher price on a consumer? What is the income effect of a higher price on a consumer?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT