This must be answered not advance methods, focusing on String method. We are working on Ch 9 in Think Java 1st Ed.I need the program to be bare bones and the coding need to be done the long way with no advanced code.
in this lab you will have two methods with headings:
- public static int countNumberSigns(String tweetText)
- public static int countHashtags(String tweetText)
'String tweetText' means the method is expectiong a string value as
an input to it.
'int' means you need to return an integer value at the end of the
method like this:
return num // num is the number of hashtags or # sign depending on
the method.
So, in the main you will call them like this:
int cNS = countNumberSigns(tweet) // cNS is the number of all #
signs in tweet
int cH= countHashtags(tweet) // cH is the number of all hashtags in
tweet
and use cNS & cH in the print statement
For quite some time now, humans have chosen to communicate in short messages called tweets. Something tweets have that other forms of speech can't compete with are hashtags. This week's program will be called HashtagCounter. If the user enters the text of a tweet, then the program will return the number of hashtags the tweet contains and the hashtag's themselves. Here is a sample run: Please enter the text of a tweet: I tweet about #coding because #c112 makes me There is(are) 2 hashtag(s) and 2 number sign(s) in your tweet. And here is another sample run: Please enter the text of a tweet: I tweet about c#oding but I sometimes make #ty#pos There is(are) 1 hashtag(s) and 3 number sign(s) in your tweet. The requirements for the program are: The static void main method will prompt the user to enter the text of a tweet using the prompt shown above in the sample run. It will contain a value method countNumberSigns(tweetText) that will take a String tweetText containing the text of a tweet and return the number of '#' characters in the tweet. It will contain a value method countHashtags(tweetText) that will take a String tweetText containing the text of a tweet and return the number of hashtags in the tweet. A hashtag is defined precisely below, but it is basically a word starting with '#'. Words that have '#' somewhere after their start aren't hashtags unless they also start with '#'. The static void main method prints the number of hashtags and number signs found in the entered text as shown in the last line of output in each of the examples above. There are many ways to write the two methods in this program. However, this is a class where we are learning to program, and we need to not use the most powerful ways we might find via Google or other routes. The only way for which you will receive credit is to use a String traversal and process the String one character at a time. We will use simplified definitions of tweets and hashtags for this program so we don't have to deal with too many special cases. A tweet is a String that contains alphanumeric characters (letters and numbers), hashtags ('#'), and spaces (' '). The part of a tweet called a hashtag is defined as non-whitespace characters following the '#' character. To be a hashtag, the '#' character must appear after a space (' ') or at the start of the tweet. The hashtag ends with either another space or the end of the tweet. Here are some
examples:
"#coding #c112 java" // contains 2 hashtags, #coding and #c112. each ends with a space "#coding java #c112" // contains 2 hashtags, #coding and #c112. #c112 ends when the String does "#c112" // 1 hashtag "c112#coding" // contains 0 hashtags because no space at start and not at start of tweet "#c112#coding" // contains 1 hashtag because the second # doesn't have a space in front of it.
package TweetTexts; import java.util.*; import java.lang.String; public class Tweets { public static void main(String[] args) { System.out.print("Please enter the text of a tweet: "); Scanner s1 = new Scanner(System.in); String tweet = s1.nextLine(); int cNS = countHashTags.countNumberSigns(tweet); int cH= countHashTags.countHashtags(tweet); System.out.println("There is(are) "+ cH+" hashtag(s) and "+cNS+" number sign(s) in your tweet"); } } class countHashTags { public static int countHashtags(String tweetText) { int count=0; String[] words = tweetText.split(" "); //split the word by space( ) for(int i=0;i<words.length;i++) { String tag = "#"; if(words[i].charAt(0) == tag.charAt(0)) //check if the 1st char of the split word "#" or not { count++; //count the HashTags } } return count; } public static int countNumberSigns(String tweetText) { int count=0; for(int i=0;i<tweetText.length();i++) { if(tweetText.charAt(i) == '#') //check all char of the string is "#" or not { count++; //count the Signs } } return count; } }
Get Answers For Free
Most questions answered within 1 hours.