Question

This must be answered not advance methods, focusing on String method. We are working on Ch...

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.

Homework Answers

Answer #1
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;
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are given a string containing a sequence of open and close brackets. and isOpen, isClose,...
You are given a string containing a sequence of open and close brackets. and isOpen, isClose, isMatching methods as defined below. Write a recursive algorithm in pseudocode for the method isBalanced that takes the  string and a stack of characters as the input and checks if the given input string contains balanced brackets. For example "(][)" would be considered not balanced where "[()]" would be considered balanced. The recursive isBalanced method utilizes  isOpen, isClose, isMatching methods as defined below. static String open...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT