Итак, моя программа должна читать текстовый файл, который содержит запись в твиттере (по одному твиту в строке). Он должен вывести количество хэштегов (любое слово, начинающееся с #), и теги имен (любое слово, начинающееся с @), и сложную часть: он должен проверять ссылки (слова во всех шапках, которые не начинаются с @ или #); затем распечатайте аббревиатуры рядом с тем, сколько они есть. Например; ввода являетсяКак считать капители аббревиатурами из текстового файла
OMG roommate @bob drank all the beer...#FML #ihatemondays
lost TV remote before superbowl #FML
Think @bieber is soo hawt...#marryme
seeing @linkinpark & @tswift in 2 weeks...OMG
Вывод должен выглядеть следующим образом:
Analyzing post:
OMG roommate @bob drank all the beer...#FML #ihatemondays
Hash tag count: 2
Name tag count: 1
Acronyms: OMG
For a total of 1 acronym(s).
Это мой код:
import java.io.*; //defines FileNotFoundException
import java.util.Scanner; // import Scanner class
public class TweetAnalyzer {
public static void main (String [] args) throws FileNotFoundException{
//variables
String tweet;
Scanner inputFile = new Scanner(new File("A3Q1-input.txt"));
while (inputFile.hasNextLine())
{
tweet = inputFile.nextLine();
System.out.println("Analyzing post: ");
System.out.println("\t" + tweet);
analyzeTweet(tweet);
}
}//close main
public static void analyzeTweet(String tweet){
int hashtags = countCharacters(tweet, '#');
int nametags = countCharacters(tweet, '@');
System.out.println("Hash tag: " + hashtags);
System.out.println("Name tag: " + nametags);
Acronyms(tweet);
}//close analyzeTweet
public static int countCharacters(String tweet, char c)//char c represents both @ and # symbols
{
int characters = 0;
char current;
for(int i=0;i<tweet.length();i++)
{
current = tweet.charAt(i);
if(current == c)
{
characters++;
}
}
return characters;
}
public static boolean symbol(String tweet, int i) {
boolean result = true;
char c;
if(i-1 >=0)
{
c = tweet.charAt(i - 1);
if (c == '@' || c == '#') {
result = false;
}
}//close if
else
{
result = false;
}
return result;
}
public static void Acronyms (String tweet){
char current;
int capital = 0;
int j = 0;
String initials = "";
for(int i = 0; i < tweet.length(); i++) {
current = tweet.charAt(i);
if(symbol(tweet, i) && current >= 'A' && current <= 'Z') {
initials += current;
j = i + 1;
current = tweet.charAt(j);
while(j < tweet.length() && current >= 'A' && current <= 'Z') {
current = tweet.charAt(j);
initials += current;
j++;
}
capital++;
i = j;
initials += " ";
}
else {
j = i + 1;
current = tweet.charAt(j);
while(j < tweet.length() && current >= 'A' && current <= 'Z') {
current = tweet.charAt(j);
j++;
}
i = j;
}
}
System.out.println(initials);
System.out.println("For a total of " + capital + " acronym(s)");
}//close Acronyms
}//TweetAnalyzer
все работает, за исключением аббревиатур части. Это мой выход:
Analyzing post:
OMG roommate @bob drank all the beer...#FML #ihatemondays
Hash tag: 2
Name tag: 1
For a total of 0 acronym(s)
Analyzing post:
lost TV remote before superbowl #FML
Hash tag: 1
Name tag: 0
For a total of 0 acronym(s)
Analyzing post:
Think @bieber is soo hawt...#marryme
Hash tag: 1
Name tag: 1
For a total of 0 acronym(s)
Analyzing post:
seeing @linkinpark & @tswift in 2 weeks...OMG
Hash tag: 0
Name tag: 2
OMG
For a total of 1 acronym(s)
пожалуйста, мне нужна помощь фиксируя аббревиатуру часть. спасибо
Вы можете написать это как ответ? спасибо –