在Java中进行推文验证

时间:2014-10-27 17:44:44

标签: java validation parsing tweets

我必须创建一个程序,接受用户的“推文”并验证它。首先,它测试以确保它少于140个字符。

如果它有效,则计算字符串中的主题标签(#),归属符号(@)和链接(“http://”)的数量,然后打印出来。我的程序适用于主题标签和归属,但不是链接。如何修复此代码以使其有效?

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);
    }
}

2 个答案:

答案 0 :(得分: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);
      }
   }
}

我通过几种方式更改了您的代码,最重要的是代替==检查链接是否以&#34; http://&#34;开头。我也使用startsWith(String s, int index)因为像@Robin所说的那样,以h开头的任何内容都可能会搞乱你的程序。

我还使用count来指定它应该从哪里开始,基本上是你参数的索引部分

您可以在Strings类javadoc中找到其他功能和文档。

答案 1 :(得分:0)

我认为您的代码不适用于http://test.com/ingex.php?p=1#section这样的链接。使用正则表达式而不是if () else {} if () else {}。请记住:没有令牌(提及,标签,链接)包含其他令牌。