0

I have to create a program that accepts a "tweet" from a user and validates it. First it tests to make sure it is less than 140 characters.

If it is valid, it counts the number of hashtags (#), attribution symbols (@), and links ("http://) are in the string, then prints them out. My program works for hashtags and attributions, but not links. How can I fix this code so that it works?

import java.util.Scanner;

class Testing {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        System.out.println("Please enter a tweet: ");
        String input = scan.nextLine();
        int length = input.length();
        int count = 0;
        int hashtags = 0, attributions = 0, links = 0;
        char letter;
        if (length > 140) {
            System.out.println("Excess characters: " + (length - 140));
        } else {
            while (count < length) {
            letter = input.charAt(count);

            if (letter =='#') {
                hashtags++;
                count++;
            }

            if (letter == '@') {
                attributions++;
                count++;
            }

            if (letter == 'h') {
                String test = input.substring(count,count+6);
                test = test.toLowerCase();
                if (test == "http://") {
                    links++;
                    count++;
                } else {
                    count++;
                }
            } else {
                count ++;
            }
        }
        System.out.println("Length Correct");
        System.out.println("Number of Hashtags: " + hashtags);
        System.out.println("Number of Attributions: " + attributions);
        System.out.println("Number of Links: " + links);
    }
}
APerson
  • 8,140
  • 8
  • 35
  • 49
cbrew
  • 1
  • 1
  • 1

2 Answers2

0
import java.util.Scanner;

class Testing
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner (System.in);
      System.out.println("Please enter a tweet: ");
      String input = scan.nextLine();
      int length = input.length();
      int count = 0;
      int hashtags = 0, attributions = 0, links = 0;
      char letter;

      if (length > 140)
      {
         System.out.println("Excess characters: " + (length - 140));
      }

      else 
      {
         while (count < length)
         {
            letter = input.charAt(count);

            if (letter =='#')
            {
               hashtags ++;
               count ++;
            }

            if (letter == '@')
            {
               attributions ++;
               count ++;
            }

            if (letter == 'h')
            {
               //String test = input.substring(count,count+6);
               //test = test.toLowerCase();
               if (input.startsWith("http://", count))
               {
                  links ++;
                  count++;
               }
               else 
               {
                  count++;
               }
            }
            else
            {
               count ++;
            }
         }
         System.out.println("Length Correct");
         System.out.println("Number of Hashtags: " + hashtags);
         System.out.println("Number of Attributions: " + attributions);
         System.out.println("Number of Links: " + links);
      }
   }
}

I changed your code in a few ways, the biggest being that instead of the == you had for checking if the links start with "http://". I also used startsWith(String s, int index) because like @Robin said, anything starting with an h would probably mess up your program.

I also used count to specify where it should start, basically the index part of you parameter

You may find additional functions and documentation in the Strings class javadoc of use.

  • 2
    The `startsWith` solution is different behavior then what was contained in the question. Now you check for each letter "h" in the tweet whether the link is placed at the beginning of the tweet – Robin Oct 27 '14 at 17:55
  • @Robin Im afraid I dont understand what you are saying – DreadHeadedDeveloper Oct 27 '14 at 17:56
  • In his original code, he loops over the whole tweet and checks each time a "h" is encountered whether it is the start of an URL. In your code, you check each time an "h" is encountered whether the whole tweet starts with an URL – Robin Oct 27 '14 at 18:01
  • @Robin if (input.startsWith("http://", count)) that checks if anything after that certain index within the String starts with "http://"? Now I'm really curious, I always thought that using count as the index would check forward from that index within the String? – DreadHeadedDeveloper Oct 27 '14 at 18:03
  • @Robin could you link to me to a place in the Java API that proves this wrong? Because I just looked up the method, I have no idea where you are getting what you are saying – DreadHeadedDeveloper Oct 27 '14 at 18:06
  • 1
    You are correct. I somehow overlooked the count. Probably because underneath your code, you mention `startsWith(String)` without the count. – Robin Oct 27 '14 at 18:11
0

I think your code will not work with link like http://test.com/ingex.php?p=1#section. Use regular expressions instead of if () else {} if () else {}. And keep in mind: there is no token(mention, hashtag, link) which contains other token.

Edward
  • 304
  • 2
  • 16