Calculate occurrence of given letter in sentence in Java [duplicate]












1
















This question already has an answer here:




  • Simple way to count character occurrences in a string [duplicate]

    15 answers




String sentence = JOptionPane.showInputDialog (null, "Write a sentence.");    
String letter = JOptionPane.showInputDialog(null, "Write a letter");

while (true) {

if (letter.equals("Stop"))
System.exit(0);
//to calculate number of specific character
else {
int countLetter = 0;
int L = letter.length();
for (int i = 0; i < L; i++) {
if ((letter.charAt(i) = .....))
countLetter++;
}
}
}


Is it possible to replace the dots to make the program count how many times the given letter occures in the sentence written in the first string?










share|improve this question















marked as duplicate by Nicholas K, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    Sure, but it's ==, not = and you may want to cycle through sentence too. In other words, you're missing a piece of code other than .....

    – Federico klez Culloca
    Nov 21 '18 at 10:44











  • Welcome to Stack Overflow, Sandra. Go through your code one more time, I think you have some errors in your thinking here. You are iterating over the characters in letter, but letter should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it with sentence in your code and then check sentence.charAt(i) == letter.charAt(0)

    – Lonely Neuron
    Nov 21 '18 at 12:59


















1
















This question already has an answer here:




  • Simple way to count character occurrences in a string [duplicate]

    15 answers




String sentence = JOptionPane.showInputDialog (null, "Write a sentence.");    
String letter = JOptionPane.showInputDialog(null, "Write a letter");

while (true) {

if (letter.equals("Stop"))
System.exit(0);
//to calculate number of specific character
else {
int countLetter = 0;
int L = letter.length();
for (int i = 0; i < L; i++) {
if ((letter.charAt(i) = .....))
countLetter++;
}
}
}


Is it possible to replace the dots to make the program count how many times the given letter occures in the sentence written in the first string?










share|improve this question















marked as duplicate by Nicholas K, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    Sure, but it's ==, not = and you may want to cycle through sentence too. In other words, you're missing a piece of code other than .....

    – Federico klez Culloca
    Nov 21 '18 at 10:44











  • Welcome to Stack Overflow, Sandra. Go through your code one more time, I think you have some errors in your thinking here. You are iterating over the characters in letter, but letter should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it with sentence in your code and then check sentence.charAt(i) == letter.charAt(0)

    – Lonely Neuron
    Nov 21 '18 at 12:59
















1












1








1









This question already has an answer here:




  • Simple way to count character occurrences in a string [duplicate]

    15 answers




String sentence = JOptionPane.showInputDialog (null, "Write a sentence.");    
String letter = JOptionPane.showInputDialog(null, "Write a letter");

while (true) {

if (letter.equals("Stop"))
System.exit(0);
//to calculate number of specific character
else {
int countLetter = 0;
int L = letter.length();
for (int i = 0; i < L; i++) {
if ((letter.charAt(i) = .....))
countLetter++;
}
}
}


Is it possible to replace the dots to make the program count how many times the given letter occures in the sentence written in the first string?










share|improve this question

















This question already has an answer here:




  • Simple way to count character occurrences in a string [duplicate]

    15 answers




String sentence = JOptionPane.showInputDialog (null, "Write a sentence.");    
String letter = JOptionPane.showInputDialog(null, "Write a letter");

while (true) {

if (letter.equals("Stop"))
System.exit(0);
//to calculate number of specific character
else {
int countLetter = 0;
int L = letter.length();
for (int i = 0; i < L; i++) {
if ((letter.charAt(i) = .....))
countLetter++;
}
}
}


Is it possible to replace the dots to make the program count how many times the given letter occures in the sentence written in the first string?





This question already has an answer here:




  • Simple way to count character occurrences in a string [duplicate]

    15 answers








java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:46









deHaar

2,36431528




2,36431528










asked Nov 21 '18 at 10:41









Sandra SköldSandra Sköld

61




61




marked as duplicate by Nicholas K, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Nicholas K, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 12:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Sure, but it's ==, not = and you may want to cycle through sentence too. In other words, you're missing a piece of code other than .....

    – Federico klez Culloca
    Nov 21 '18 at 10:44











  • Welcome to Stack Overflow, Sandra. Go through your code one more time, I think you have some errors in your thinking here. You are iterating over the characters in letter, but letter should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it with sentence in your code and then check sentence.charAt(i) == letter.charAt(0)

    – Lonely Neuron
    Nov 21 '18 at 12:59
















  • 1





    Sure, but it's ==, not = and you may want to cycle through sentence too. In other words, you're missing a piece of code other than .....

    – Federico klez Culloca
    Nov 21 '18 at 10:44











  • Welcome to Stack Overflow, Sandra. Go through your code one more time, I think you have some errors in your thinking here. You are iterating over the characters in letter, but letter should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it with sentence in your code and then check sentence.charAt(i) == letter.charAt(0)

    – Lonely Neuron
    Nov 21 '18 at 12:59










1




1





Sure, but it's ==, not = and you may want to cycle through sentence too. In other words, you're missing a piece of code other than .....

– Federico klez Culloca
Nov 21 '18 at 10:44





Sure, but it's ==, not = and you may want to cycle through sentence too. In other words, you're missing a piece of code other than .....

– Federico klez Culloca
Nov 21 '18 at 10:44













Welcome to Stack Overflow, Sandra. Go through your code one more time, I think you have some errors in your thinking here. You are iterating over the characters in letter, but letter should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it with sentence in your code and then check sentence.charAt(i) == letter.charAt(0)

– Lonely Neuron
Nov 21 '18 at 12:59







Welcome to Stack Overflow, Sandra. Go through your code one more time, I think you have some errors in your thinking here. You are iterating over the characters in letter, but letter should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it with sentence in your code and then check sentence.charAt(i) == letter.charAt(0)

– Lonely Neuron
Nov 21 '18 at 12:59














4 Answers
4






active

oldest

votes


















3














Since Java 8, there is an elegant solution to this.



int count = letter.chars().filter(ch -> ch == 'e').count();


This will return the number of occurences of letter 'e'.






share|improve this answer































    0














    if your String letter contains a one character use this letter.charAt(0) and then replace dots with this. Also remember to use == instead of = here. = means you are just asigning and == uses to compare two values.






    share|improve this answer































      0














      If you have to use a for loop and want to stick to the old fashioned way, try this:



          String sentence = "This is a really basic sentence, just for example purpose.";
      char letter = 'a';

      int occurrenceOfChar = 0;

      for (int i = 0; i < sentence.length(); i++) {
      if (sentence.charAt(i) == letter) {
      occurrenceOfChar++;
      }
      }

      System.out.println("The letter '" + letter
      + "' occurs " + occurrenceOfChar
      + " times in the sentence ""
      + sentence + """);



      The sentence and the letter are just examples, you have to read the user input.







      share|improve this answer































        0














        You can use Guava Lib to perform this operation faster without iterating string.



        CharMatcher.is('e').countIn("Write a letter");


        Will return 3






        share|improve this answer
























        • Frankly I would advise against adding an external library for something this trivial.

          – Federico klez Culloca
          Nov 21 '18 at 11:00











        • You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

          – Ravi Sapariya
          Nov 21 '18 at 12:17













        • I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

          – Lonely Neuron
          Nov 21 '18 at 12:56


















        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        Since Java 8, there is an elegant solution to this.



        int count = letter.chars().filter(ch -> ch == 'e').count();


        This will return the number of occurences of letter 'e'.






        share|improve this answer




























          3














          Since Java 8, there is an elegant solution to this.



          int count = letter.chars().filter(ch -> ch == 'e').count();


          This will return the number of occurences of letter 'e'.






          share|improve this answer


























            3












            3








            3







            Since Java 8, there is an elegant solution to this.



            int count = letter.chars().filter(ch -> ch == 'e').count();


            This will return the number of occurences of letter 'e'.






            share|improve this answer













            Since Java 8, there is an elegant solution to this.



            int count = letter.chars().filter(ch -> ch == 'e').count();


            This will return the number of occurences of letter 'e'.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 10:46









            user43648user43648

            1349




            1349

























                0














                if your String letter contains a one character use this letter.charAt(0) and then replace dots with this. Also remember to use == instead of = here. = means you are just asigning and == uses to compare two values.






                share|improve this answer




























                  0














                  if your String letter contains a one character use this letter.charAt(0) and then replace dots with this. Also remember to use == instead of = here. = means you are just asigning and == uses to compare two values.






                  share|improve this answer


























                    0












                    0








                    0







                    if your String letter contains a one character use this letter.charAt(0) and then replace dots with this. Also remember to use == instead of = here. = means you are just asigning and == uses to compare two values.






                    share|improve this answer













                    if your String letter contains a one character use this letter.charAt(0) and then replace dots with this. Also remember to use == instead of = here. = means you are just asigning and == uses to compare two values.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 10:44









                    SandSand

                    1,431319




                    1,431319























                        0














                        If you have to use a for loop and want to stick to the old fashioned way, try this:



                            String sentence = "This is a really basic sentence, just for example purpose.";
                        char letter = 'a';

                        int occurrenceOfChar = 0;

                        for (int i = 0; i < sentence.length(); i++) {
                        if (sentence.charAt(i) == letter) {
                        occurrenceOfChar++;
                        }
                        }

                        System.out.println("The letter '" + letter
                        + "' occurs " + occurrenceOfChar
                        + " times in the sentence ""
                        + sentence + """);



                        The sentence and the letter are just examples, you have to read the user input.







                        share|improve this answer




























                          0














                          If you have to use a for loop and want to stick to the old fashioned way, try this:



                              String sentence = "This is a really basic sentence, just for example purpose.";
                          char letter = 'a';

                          int occurrenceOfChar = 0;

                          for (int i = 0; i < sentence.length(); i++) {
                          if (sentence.charAt(i) == letter) {
                          occurrenceOfChar++;
                          }
                          }

                          System.out.println("The letter '" + letter
                          + "' occurs " + occurrenceOfChar
                          + " times in the sentence ""
                          + sentence + """);



                          The sentence and the letter are just examples, you have to read the user input.







                          share|improve this answer


























                            0












                            0








                            0







                            If you have to use a for loop and want to stick to the old fashioned way, try this:



                                String sentence = "This is a really basic sentence, just for example purpose.";
                            char letter = 'a';

                            int occurrenceOfChar = 0;

                            for (int i = 0; i < sentence.length(); i++) {
                            if (sentence.charAt(i) == letter) {
                            occurrenceOfChar++;
                            }
                            }

                            System.out.println("The letter '" + letter
                            + "' occurs " + occurrenceOfChar
                            + " times in the sentence ""
                            + sentence + """);



                            The sentence and the letter are just examples, you have to read the user input.







                            share|improve this answer













                            If you have to use a for loop and want to stick to the old fashioned way, try this:



                                String sentence = "This is a really basic sentence, just for example purpose.";
                            char letter = 'a';

                            int occurrenceOfChar = 0;

                            for (int i = 0; i < sentence.length(); i++) {
                            if (sentence.charAt(i) == letter) {
                            occurrenceOfChar++;
                            }
                            }

                            System.out.println("The letter '" + letter
                            + "' occurs " + occurrenceOfChar
                            + " times in the sentence ""
                            + sentence + """);



                            The sentence and the letter are just examples, you have to read the user input.








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 10:49









                            deHaardeHaar

                            2,36431528




                            2,36431528























                                0














                                You can use Guava Lib to perform this operation faster without iterating string.



                                CharMatcher.is('e').countIn("Write a letter");


                                Will return 3






                                share|improve this answer
























                                • Frankly I would advise against adding an external library for something this trivial.

                                  – Federico klez Culloca
                                  Nov 21 '18 at 11:00











                                • You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

                                  – Ravi Sapariya
                                  Nov 21 '18 at 12:17













                                • I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

                                  – Lonely Neuron
                                  Nov 21 '18 at 12:56
















                                0














                                You can use Guava Lib to perform this operation faster without iterating string.



                                CharMatcher.is('e').countIn("Write a letter");


                                Will return 3






                                share|improve this answer
























                                • Frankly I would advise against adding an external library for something this trivial.

                                  – Federico klez Culloca
                                  Nov 21 '18 at 11:00











                                • You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

                                  – Ravi Sapariya
                                  Nov 21 '18 at 12:17













                                • I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

                                  – Lonely Neuron
                                  Nov 21 '18 at 12:56














                                0












                                0








                                0







                                You can use Guava Lib to perform this operation faster without iterating string.



                                CharMatcher.is('e').countIn("Write a letter");


                                Will return 3






                                share|improve this answer













                                You can use Guava Lib to perform this operation faster without iterating string.



                                CharMatcher.is('e').countIn("Write a letter");


                                Will return 3







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 21 '18 at 10:53









                                Ravi SapariyaRavi Sapariya

                                1828




                                1828













                                • Frankly I would advise against adding an external library for something this trivial.

                                  – Federico klez Culloca
                                  Nov 21 '18 at 11:00











                                • You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

                                  – Ravi Sapariya
                                  Nov 21 '18 at 12:17













                                • I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

                                  – Lonely Neuron
                                  Nov 21 '18 at 12:56



















                                • Frankly I would advise against adding an external library for something this trivial.

                                  – Federico klez Culloca
                                  Nov 21 '18 at 11:00











                                • You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

                                  – Ravi Sapariya
                                  Nov 21 '18 at 12:17













                                • I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

                                  – Lonely Neuron
                                  Nov 21 '18 at 12:56

















                                Frankly I would advise against adding an external library for something this trivial.

                                – Federico klez Culloca
                                Nov 21 '18 at 11:00





                                Frankly I would advise against adding an external library for something this trivial.

                                – Federico klez Culloca
                                Nov 21 '18 at 11:00













                                You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

                                – Ravi Sapariya
                                Nov 21 '18 at 12:17







                                You are right but i am just suggesting if they might use the same Lib. @Federico klez Culloca

                                – Ravi Sapariya
                                Nov 21 '18 at 12:17















                                I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

                                – Lonely Neuron
                                Nov 21 '18 at 12:56





                                I don't think OP is at a point at learning the language, where using libraries is detrimental to their learning progress

                                – Lonely Neuron
                                Nov 21 '18 at 12:56



                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin