How to find out the percentage of vowels in a string?












-1














package scanner;
import java.util.Scanner;

public class GuessSentence {
public static void main(String args) {
Scanner sc = new Scanner(System.in);

System.out.println("Type a sentence");
String sentence = sc.nextLine();
System.out.println("You entered the sentence " + sentence);
System.out.println("The number of words in the sentence is " + sentence.length());

char chars=sentence.toCharArray();

int count = 0;
for (char c : chars) {
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
}
System.out.println("The numner of vowels in your sentence is " + count);
System.out.println("The percentage of vowels is " + 100 * count /sentence.length() + "%" );
}
}


Thank you to everyone who helped, I was able to get the correct outcome which i was looking for so i appreciate all the help recieved.










share|improve this question
























  • "However, i do not get the percentage of vowels" Well, it is there in the output The percentage of vowels is 2%. Do you mean that it is just not correct?
    – csmckelvey
    Nov 21 '18 at 17:36












  • Possible duplicate of Why is the result of 1/3 == 0?
    – MWB
    Nov 21 '18 at 17:38
















-1














package scanner;
import java.util.Scanner;

public class GuessSentence {
public static void main(String args) {
Scanner sc = new Scanner(System.in);

System.out.println("Type a sentence");
String sentence = sc.nextLine();
System.out.println("You entered the sentence " + sentence);
System.out.println("The number of words in the sentence is " + sentence.length());

char chars=sentence.toCharArray();

int count = 0;
for (char c : chars) {
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
}
System.out.println("The numner of vowels in your sentence is " + count);
System.out.println("The percentage of vowels is " + 100 * count /sentence.length() + "%" );
}
}


Thank you to everyone who helped, I was able to get the correct outcome which i was looking for so i appreciate all the help recieved.










share|improve this question
























  • "However, i do not get the percentage of vowels" Well, it is there in the output The percentage of vowels is 2%. Do you mean that it is just not correct?
    – csmckelvey
    Nov 21 '18 at 17:36












  • Possible duplicate of Why is the result of 1/3 == 0?
    – MWB
    Nov 21 '18 at 17:38














-1












-1








-1







package scanner;
import java.util.Scanner;

public class GuessSentence {
public static void main(String args) {
Scanner sc = new Scanner(System.in);

System.out.println("Type a sentence");
String sentence = sc.nextLine();
System.out.println("You entered the sentence " + sentence);
System.out.println("The number of words in the sentence is " + sentence.length());

char chars=sentence.toCharArray();

int count = 0;
for (char c : chars) {
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
}
System.out.println("The numner of vowels in your sentence is " + count);
System.out.println("The percentage of vowels is " + 100 * count /sentence.length() + "%" );
}
}


Thank you to everyone who helped, I was able to get the correct outcome which i was looking for so i appreciate all the help recieved.










share|improve this question















package scanner;
import java.util.Scanner;

public class GuessSentence {
public static void main(String args) {
Scanner sc = new Scanner(System.in);

System.out.println("Type a sentence");
String sentence = sc.nextLine();
System.out.println("You entered the sentence " + sentence);
System.out.println("The number of words in the sentence is " + sentence.length());

char chars=sentence.toCharArray();

int count = 0;
for (char c : chars) {
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
}
System.out.println("The numner of vowels in your sentence is " + count);
System.out.println("The percentage of vowels is " + 100 * count /sentence.length() + "%" );
}
}


Thank you to everyone who helped, I was able to get the correct outcome which i was looking for so i appreciate all the help recieved.







java eclipse java.util.scanner






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:52

























asked Nov 21 '18 at 17:33









ahmed2993

13




13












  • "However, i do not get the percentage of vowels" Well, it is there in the output The percentage of vowels is 2%. Do you mean that it is just not correct?
    – csmckelvey
    Nov 21 '18 at 17:36












  • Possible duplicate of Why is the result of 1/3 == 0?
    – MWB
    Nov 21 '18 at 17:38


















  • "However, i do not get the percentage of vowels" Well, it is there in the output The percentage of vowels is 2%. Do you mean that it is just not correct?
    – csmckelvey
    Nov 21 '18 at 17:36












  • Possible duplicate of Why is the result of 1/3 == 0?
    – MWB
    Nov 21 '18 at 17:38
















"However, i do not get the percentage of vowels" Well, it is there in the output The percentage of vowels is 2%. Do you mean that it is just not correct?
– csmckelvey
Nov 21 '18 at 17:36






"However, i do not get the percentage of vowels" Well, it is there in the output The percentage of vowels is 2%. Do you mean that it is just not correct?
– csmckelvey
Nov 21 '18 at 17:36














Possible duplicate of Why is the result of 1/3 == 0?
– MWB
Nov 21 '18 at 17:38




Possible duplicate of Why is the result of 1/3 == 0?
– MWB
Nov 21 '18 at 17:38












3 Answers
3






active

oldest

votes


















2














You want (100.0 * count / sentence.length()). You're using the % operator which is the modulo of two numbers






share|improve this answer





























    1














    When you calculate the percent you do:



    sentence.length() % count


    But % is the modulo operator, which calculates remainder. You wanted to divide:



    sentence.length() / count


    However this will still not get you the right results as the scale is not right, and you are dividing incorrectly. It should be:



    100 *count / sentence.length()


    Or



    100.0 *count / sentence.length() 


    If you want to avoid truncation



    Output:



    You entered the sentence Hello World
    The number of words in the sentence is 11
    The numner of vowels in your sentence is 3
    The percentage of vowels is 27%





    share|improve this answer























    • When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
      – flakes
      Nov 21 '18 at 17:41










    • @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
      – Dawood ibn Kareem
      Nov 21 '18 at 17:42










    • i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
      – ahmed2993
      Nov 21 '18 at 17:48










    • @Satoshi1999 What do you mean by add a float?
      – GBlodgett
      Nov 21 '18 at 17:49






    • 1




      Nevermind, just made the correct changes and everything is correct, thank you
      – ahmed2993
      Nov 21 '18 at 17:51



















    0














    You are not using correct operator. modulus(%) gives you the remainder after the division. You need to use division (/) operation. You may want to use double/float for getting accurate value.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417687%2fhow-to-find-out-the-percentage-of-vowels-in-a-string%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You want (100.0 * count / sentence.length()). You're using the % operator which is the modulo of two numbers






      share|improve this answer


























        2














        You want (100.0 * count / sentence.length()). You're using the % operator which is the modulo of two numbers






        share|improve this answer
























          2












          2








          2






          You want (100.0 * count / sentence.length()). You're using the % operator which is the modulo of two numbers






          share|improve this answer












          You want (100.0 * count / sentence.length()). You're using the % operator which is the modulo of two numbers







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 17:37









          flakes

          6,52111850




          6,52111850

























              1














              When you calculate the percent you do:



              sentence.length() % count


              But % is the modulo operator, which calculates remainder. You wanted to divide:



              sentence.length() / count


              However this will still not get you the right results as the scale is not right, and you are dividing incorrectly. It should be:



              100 *count / sentence.length()


              Or



              100.0 *count / sentence.length() 


              If you want to avoid truncation



              Output:



              You entered the sentence Hello World
              The number of words in the sentence is 11
              The numner of vowels in your sentence is 3
              The percentage of vowels is 27%





              share|improve this answer























              • When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
                – flakes
                Nov 21 '18 at 17:41










              • @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
                – Dawood ibn Kareem
                Nov 21 '18 at 17:42










              • i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
                – ahmed2993
                Nov 21 '18 at 17:48










              • @Satoshi1999 What do you mean by add a float?
                – GBlodgett
                Nov 21 '18 at 17:49






              • 1




                Nevermind, just made the correct changes and everything is correct, thank you
                – ahmed2993
                Nov 21 '18 at 17:51
















              1














              When you calculate the percent you do:



              sentence.length() % count


              But % is the modulo operator, which calculates remainder. You wanted to divide:



              sentence.length() / count


              However this will still not get you the right results as the scale is not right, and you are dividing incorrectly. It should be:



              100 *count / sentence.length()


              Or



              100.0 *count / sentence.length() 


              If you want to avoid truncation



              Output:



              You entered the sentence Hello World
              The number of words in the sentence is 11
              The numner of vowels in your sentence is 3
              The percentage of vowels is 27%





              share|improve this answer























              • When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
                – flakes
                Nov 21 '18 at 17:41










              • @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
                – Dawood ibn Kareem
                Nov 21 '18 at 17:42










              • i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
                – ahmed2993
                Nov 21 '18 at 17:48










              • @Satoshi1999 What do you mean by add a float?
                – GBlodgett
                Nov 21 '18 at 17:49






              • 1




                Nevermind, just made the correct changes and everything is correct, thank you
                – ahmed2993
                Nov 21 '18 at 17:51














              1












              1








              1






              When you calculate the percent you do:



              sentence.length() % count


              But % is the modulo operator, which calculates remainder. You wanted to divide:



              sentence.length() / count


              However this will still not get you the right results as the scale is not right, and you are dividing incorrectly. It should be:



              100 *count / sentence.length()


              Or



              100.0 *count / sentence.length() 


              If you want to avoid truncation



              Output:



              You entered the sentence Hello World
              The number of words in the sentence is 11
              The numner of vowels in your sentence is 3
              The percentage of vowels is 27%





              share|improve this answer














              When you calculate the percent you do:



              sentence.length() % count


              But % is the modulo operator, which calculates remainder. You wanted to divide:



              sentence.length() / count


              However this will still not get you the right results as the scale is not right, and you are dividing incorrectly. It should be:



              100 *count / sentence.length()


              Or



              100.0 *count / sentence.length() 


              If you want to avoid truncation



              Output:



              You entered the sentence Hello World
              The number of words in the sentence is 11
              The numner of vowels in your sentence is 3
              The percentage of vowels is 27%






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 21 '18 at 17:43

























              answered Nov 21 '18 at 17:36









              GBlodgett

              9,31041633




              9,31041633












              • When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
                – flakes
                Nov 21 '18 at 17:41










              • @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
                – Dawood ibn Kareem
                Nov 21 '18 at 17:42










              • i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
                – ahmed2993
                Nov 21 '18 at 17:48










              • @Satoshi1999 What do you mean by add a float?
                – GBlodgett
                Nov 21 '18 at 17:49






              • 1




                Nevermind, just made the correct changes and everything is correct, thank you
                – ahmed2993
                Nov 21 '18 at 17:51


















              • When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
                – flakes
                Nov 21 '18 at 17:41










              • @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
                – Dawood ibn Kareem
                Nov 21 '18 at 17:42










              • i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
                – ahmed2993
                Nov 21 '18 at 17:48










              • @Satoshi1999 What do you mean by add a float?
                – GBlodgett
                Nov 21 '18 at 17:49






              • 1




                Nevermind, just made the correct changes and everything is correct, thank you
                – ahmed2993
                Nov 21 '18 at 17:51
















              When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
              – flakes
              Nov 21 '18 at 17:41




              When dealing with percentage its usually better to use a floating point 100.0 rather than an integer to avoid truncation.
              – flakes
              Nov 21 '18 at 17:41












              @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
              – Dawood ibn Kareem
              Nov 21 '18 at 17:42




              @flakes Not necessarily. Rounding to the next integer below is probably exactly what OP wants.
              – Dawood ibn Kareem
              Nov 21 '18 at 17:42












              i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
              – ahmed2993
              Nov 21 '18 at 17:48




              i also believe i need to add a float, but do not know where i would add this, the code you wrote has helped me so thank you
              – ahmed2993
              Nov 21 '18 at 17:48












              @Satoshi1999 What do you mean by add a float?
              – GBlodgett
              Nov 21 '18 at 17:49




              @Satoshi1999 What do you mean by add a float?
              – GBlodgett
              Nov 21 '18 at 17:49




              1




              1




              Nevermind, just made the correct changes and everything is correct, thank you
              – ahmed2993
              Nov 21 '18 at 17:51




              Nevermind, just made the correct changes and everything is correct, thank you
              – ahmed2993
              Nov 21 '18 at 17:51











              0














              You are not using correct operator. modulus(%) gives you the remainder after the division. You need to use division (/) operation. You may want to use double/float for getting accurate value.






              share|improve this answer


























                0














                You are not using correct operator. modulus(%) gives you the remainder after the division. You need to use division (/) operation. You may want to use double/float for getting accurate value.






                share|improve this answer
























                  0












                  0








                  0






                  You are not using correct operator. modulus(%) gives you the remainder after the division. You need to use division (/) operation. You may want to use double/float for getting accurate value.






                  share|improve this answer












                  You are not using correct operator. modulus(%) gives you the remainder after the division. You need to use division (/) operation. You may want to use double/float for getting accurate value.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 17:37









                  Sachin Gupta

                  629211




                  629211






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417687%2fhow-to-find-out-the-percentage-of-vowels-in-a-string%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python