Java中的Tweet验证
我必须创建一个程序,接受用户的推文并对其进行验证。首先,它进行测试以确保少于140个字符 如果有效,它会计算字符串中的哈希标记、属性符号@和链接http://的数量,然后打印出来。我的程序适用于哈希标记和属性,但不适用于链接。如何修复此代码以使其正常工作Java中的Tweet验证,java,validation,parsing,tweets,Java,Validation,Parsing,Tweets,我必须创建一个程序,接受用户的推文并对其进行验证。首先,它进行测试以确保少于140个字符 如果有效,它会计算字符串中的哈希标记、属性符号@和链接http://的数量,然后打印出来。我的程序适用于哈希标记和属性,但不适用于链接。如何修复此代码以使其正常工作 import java.util.Scanner; class Testing { public static void main(String[] args) { Scanner scan = new Scanner
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);
}
}
我以几种方式更改了您的代码,其中最大的一种方式是使用==来检查链接是否以http://开头。我还使用了startsWithString s,int index,因为正如@Robin所说的,任何以h开头的东西都可能会打乱你的程序
我还使用count指定它应该从哪里开始,基本上是参数的索引部分
您可能会在使用的javadoc类中找到其他函数和文档。我认为您的代码将无法与link-like一起工作。使用正则表达式而不是if-else{}if-else{}。请记住:没有包含其他令牌的令牌提及、hashtag和link。您能举例说明链接不起作用的地方吗?不要使用==在比较字符串时,使用。等式注意,以字母h结尾的tweet消息会使您的程序因输入而崩溃。substringcount,count+6;statement@DreadHeadedDeveloper这是正确的。有关更详细的解释,请参阅。您可以将count++公因子化,并将其放在循环的末尾,而不是放在每个条件语句中。StartWith解决方案的行为与问题中包含的行为不同。现在,您检查tweet中的每个字母h,看链接是否位于tweet的开头tweet@Robin恐怕我不明白你在说什么在他的原始代码中,他在整个推特上循环,每次遇到h时都检查它是否是URL的开头。在代码中,每次遇到h时,您都会检查整个tweet是否以URL@Robin如果input.STARTSWITHttp://,则检查字符串中该索引之后的任何内容是否以http://开头的计数?现在我真的很好奇,我一直认为使用count作为索引会从字符串中的索引向前检查?你是对的。不知怎的,我忽略了伯爵。可能是因为在你的代码下面,你提到了startsWithString而没有计数。
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);
}
}
}