-1

I have below mentioned string

String str = "\nArticle\n\nArticle\nArticle";

I want total number of count. How can i get this? As the string contain \n so always it gives 1 instead of 3

Question Warriors
  • 117
  • 1
  • 1
  • 12
  • How do you do it in the first place? How can we correct the code you've produced without seeing it. – Yassin Hajaj Jul 04 '16 at 18:16
  • Count of words separated by \n? – κροκς Jul 04 '16 at 18:17
  • 2
    Possible Duplicate of [1]:http://stackoverflow.com/questions/2850203/count-the-number-of-lines-in-a-java-string and [2]http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string – S.Neum Jul 04 '16 at 18:18
  • 1
    Reviewer here: can you expand your question a little? You'll get more positive feedback to your question if you include what you've tried so far, what you've researched/Googled and what errors/problems you're encountering with your current solution. –  Jul 04 '16 at 20:38

1 Answers1

0

To get you started, I will show you a simple example:

String str = "\nArticle\n\nArticle\nArticle";

// Split the String by \n
String[] words = str.split("\n");

// Keep the count of words
int wordCount = 0;

for(String word : words){
   // Only count non-empty Strings
   if(!word.isEmpty()) {
       wordCount++;
   }
}

// Check, answer is 3
System.out.println(wordCount);
MaxXFrenzY
  • 51
  • 7