Would need a little bit of clarification









up vote
0
down vote

favorite












Can you guys, please, explain to me what does count[word.charAt(i)]++, exactly do in this code and overall--?



public static void main(String args) 
String S = "Some random text to test.";
int count = new int[124];
for (int i=0; i< S.length(); i++)
count[S.charAt(i)]++;
System.out.print(count[S.charAt(i)] + " ");

int max = 1;
char result = ' ';

for (int i = 0; i < S.length(); i++)
if (max < count[S.charAt(i)] && S.charAt(i) != ' ')
max = count[S.charAt(i)];
result = S.charAt(i);


System.out.println(result);



The printing of count[S.charAt(i)] was just me trying to figure it out.










share|improve this question



















  • 1




    The count array is being used as a map of sorts, to keep track of how many times each character in S occurs. This is just a hint. If you step through your code and poke around, you'll see this.
    – Tim Biegeleisen
    Nov 11 at 14:25










  • It might be better here to use 128 instead of 124, since otherwise for |, } and ~ this will produce an error.
    – Willem Van Onsem
    Nov 11 at 14:27














up vote
0
down vote

favorite












Can you guys, please, explain to me what does count[word.charAt(i)]++, exactly do in this code and overall--?



public static void main(String args) 
String S = "Some random text to test.";
int count = new int[124];
for (int i=0; i< S.length(); i++)
count[S.charAt(i)]++;
System.out.print(count[S.charAt(i)] + " ");

int max = 1;
char result = ' ';

for (int i = 0; i < S.length(); i++)
if (max < count[S.charAt(i)] && S.charAt(i) != ' ')
max = count[S.charAt(i)];
result = S.charAt(i);


System.out.println(result);



The printing of count[S.charAt(i)] was just me trying to figure it out.










share|improve this question



















  • 1




    The count array is being used as a map of sorts, to keep track of how many times each character in S occurs. This is just a hint. If you step through your code and poke around, you'll see this.
    – Tim Biegeleisen
    Nov 11 at 14:25










  • It might be better here to use 128 instead of 124, since otherwise for |, } and ~ this will produce an error.
    – Willem Van Onsem
    Nov 11 at 14:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Can you guys, please, explain to me what does count[word.charAt(i)]++, exactly do in this code and overall--?



public static void main(String args) 
String S = "Some random text to test.";
int count = new int[124];
for (int i=0; i< S.length(); i++)
count[S.charAt(i)]++;
System.out.print(count[S.charAt(i)] + " ");

int max = 1;
char result = ' ';

for (int i = 0; i < S.length(); i++)
if (max < count[S.charAt(i)] && S.charAt(i) != ' ')
max = count[S.charAt(i)];
result = S.charAt(i);


System.out.println(result);



The printing of count[S.charAt(i)] was just me trying to figure it out.










share|improve this question















Can you guys, please, explain to me what does count[word.charAt(i)]++, exactly do in this code and overall--?



public static void main(String args) 
String S = "Some random text to test.";
int count = new int[124];
for (int i=0; i< S.length(); i++)
count[S.charAt(i)]++;
System.out.print(count[S.charAt(i)] + " ");

int max = 1;
char result = ' ';

for (int i = 0; i < S.length(); i++)
if (max < count[S.charAt(i)] && S.charAt(i) != ' ')
max = count[S.charAt(i)];
result = S.charAt(i);


System.out.println(result);



The printing of count[S.charAt(i)] was just me trying to figure it out.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 3:32









Nadim Baraky

427516




427516










asked Nov 11 at 14:20









Steliqn Nedev

1




1







  • 1




    The count array is being used as a map of sorts, to keep track of how many times each character in S occurs. This is just a hint. If you step through your code and poke around, you'll see this.
    – Tim Biegeleisen
    Nov 11 at 14:25










  • It might be better here to use 128 instead of 124, since otherwise for |, } and ~ this will produce an error.
    – Willem Van Onsem
    Nov 11 at 14:27












  • 1




    The count array is being used as a map of sorts, to keep track of how many times each character in S occurs. This is just a hint. If you step through your code and poke around, you'll see this.
    – Tim Biegeleisen
    Nov 11 at 14:25










  • It might be better here to use 128 instead of 124, since otherwise for |, } and ~ this will produce an error.
    – Willem Van Onsem
    Nov 11 at 14:27







1




1




The count array is being used as a map of sorts, to keep track of how many times each character in S occurs. This is just a hint. If you step through your code and poke around, you'll see this.
– Tim Biegeleisen
Nov 11 at 14:25




The count array is being used as a map of sorts, to keep track of how many times each character in S occurs. This is just a hint. If you step through your code and poke around, you'll see this.
– Tim Biegeleisen
Nov 11 at 14:25












It might be better here to use 128 instead of 124, since otherwise for |, } and ~ this will produce an error.
– Willem Van Onsem
Nov 11 at 14:27




It might be better here to use 128 instead of 124, since otherwise for |, } and ~ this will produce an error.
– Willem Van Onsem
Nov 11 at 14:27












2 Answers
2






active

oldest

votes

















up vote
0
down vote













S.charAt(i) returns the character in the i-th position of that string S.



Then count[S.charAt(i)] will execute like this. Lets say you get 'S' as the character. Then the character value for 'S' will be 83. So, it will take the element of the 83 index in count array and increment it by one.






share|improve this answer





























    up vote
    0
    down vote













    word.charAt(i) returns the character at the i-th index in the String word.



    count is an int array with all zeros automatically : int count = new int[124];



    count[i]++ increments the value that is in count at index i by 1.



    Here, you're passing word.charAt(i) as an index i.e count[word.charAt(i)]++, and what it does is:



    -Evaluate word.charAt(i) first, but
    note that index i must be an integer!

    so automatically gets the ASCII value of the character. For example ('a' = 97, 'b' = 98..)



    -then, count[ASCII number returned]++, for example count[97]++, will be incremented and now count[97] = 1



    But note that if your String has '}', there will be Index out of bound exception, since its ASCII value is 125; and 125 > 124 the size of count!






    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',
      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%2f53249633%2fwould-need-a-little-bit-of-clarification%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      S.charAt(i) returns the character in the i-th position of that string S.



      Then count[S.charAt(i)] will execute like this. Lets say you get 'S' as the character. Then the character value for 'S' will be 83. So, it will take the element of the 83 index in count array and increment it by one.






      share|improve this answer


























        up vote
        0
        down vote













        S.charAt(i) returns the character in the i-th position of that string S.



        Then count[S.charAt(i)] will execute like this. Lets say you get 'S' as the character. Then the character value for 'S' will be 83. So, it will take the element of the 83 index in count array and increment it by one.






        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          S.charAt(i) returns the character in the i-th position of that string S.



          Then count[S.charAt(i)] will execute like this. Lets say you get 'S' as the character. Then the character value for 'S' will be 83. So, it will take the element of the 83 index in count array and increment it by one.






          share|improve this answer














          S.charAt(i) returns the character in the i-th position of that string S.



          Then count[S.charAt(i)] will execute like this. Lets say you get 'S' as the character. Then the character value for 'S' will be 83. So, it will take the element of the 83 index in count array and increment it by one.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 19:03









          Nadim Baraky

          427516




          427516










          answered Nov 11 at 14:28









          Sand

          7259




          7259






















              up vote
              0
              down vote













              word.charAt(i) returns the character at the i-th index in the String word.



              count is an int array with all zeros automatically : int count = new int[124];



              count[i]++ increments the value that is in count at index i by 1.



              Here, you're passing word.charAt(i) as an index i.e count[word.charAt(i)]++, and what it does is:



              -Evaluate word.charAt(i) first, but
              note that index i must be an integer!

              so automatically gets the ASCII value of the character. For example ('a' = 97, 'b' = 98..)



              -then, count[ASCII number returned]++, for example count[97]++, will be incremented and now count[97] = 1



              But note that if your String has '}', there will be Index out of bound exception, since its ASCII value is 125; and 125 > 124 the size of count!






              share|improve this answer
























                up vote
                0
                down vote













                word.charAt(i) returns the character at the i-th index in the String word.



                count is an int array with all zeros automatically : int count = new int[124];



                count[i]++ increments the value that is in count at index i by 1.



                Here, you're passing word.charAt(i) as an index i.e count[word.charAt(i)]++, and what it does is:



                -Evaluate word.charAt(i) first, but
                note that index i must be an integer!

                so automatically gets the ASCII value of the character. For example ('a' = 97, 'b' = 98..)



                -then, count[ASCII number returned]++, for example count[97]++, will be incremented and now count[97] = 1



                But note that if your String has '}', there will be Index out of bound exception, since its ASCII value is 125; and 125 > 124 the size of count!






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  word.charAt(i) returns the character at the i-th index in the String word.



                  count is an int array with all zeros automatically : int count = new int[124];



                  count[i]++ increments the value that is in count at index i by 1.



                  Here, you're passing word.charAt(i) as an index i.e count[word.charAt(i)]++, and what it does is:



                  -Evaluate word.charAt(i) first, but
                  note that index i must be an integer!

                  so automatically gets the ASCII value of the character. For example ('a' = 97, 'b' = 98..)



                  -then, count[ASCII number returned]++, for example count[97]++, will be incremented and now count[97] = 1



                  But note that if your String has '}', there will be Index out of bound exception, since its ASCII value is 125; and 125 > 124 the size of count!






                  share|improve this answer












                  word.charAt(i) returns the character at the i-th index in the String word.



                  count is an int array with all zeros automatically : int count = new int[124];



                  count[i]++ increments the value that is in count at index i by 1.



                  Here, you're passing word.charAt(i) as an index i.e count[word.charAt(i)]++, and what it does is:



                  -Evaluate word.charAt(i) first, but
                  note that index i must be an integer!

                  so automatically gets the ASCII value of the character. For example ('a' = 97, 'b' = 98..)



                  -then, count[ASCII number returned]++, for example count[97]++, will be incremented and now count[97] = 1



                  But note that if your String has '}', there will be Index out of bound exception, since its ASCII value is 125; and 125 > 124 the size of count!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 19:22









                  Nadim Baraky

                  427516




                  427516



























                      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%2f53249633%2fwould-need-a-little-bit-of-clarification%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







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3