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.
java
add a comment |
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.
java
1
Thecount
array is being used as a map of sorts, to keep track of how many times each character inS
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 use128
instead of124
, since otherwise for|
,}
and~
this will produce an error.
– Willem Van Onsem
Nov 11 at 14:27
add a comment |
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.
java
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
java
edited Nov 12 at 3:32
Nadim Baraky
427516
427516
asked Nov 11 at 14:20
Steliqn Nedev
1
1
1
Thecount
array is being used as a map of sorts, to keep track of how many times each character inS
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 use128
instead of124
, since otherwise for|
,}
and~
this will produce an error.
– Willem Van Onsem
Nov 11 at 14:27
add a comment |
1
Thecount
array is being used as a map of sorts, to keep track of how many times each character inS
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 use128
instead of124
, 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
add a comment |
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.
add a comment |
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!
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 11 at 19:03
Nadim Baraky
427516
427516
answered Nov 11 at 14:28
Sand
7259
7259
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Nov 11 at 19:22
Nadim Baraky
427516
427516
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
The
count
array is being used as a map of sorts, to keep track of how many times each character inS
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 of124
, since otherwise for|
,}
and~
this will produce an error.– Willem Van Onsem
Nov 11 at 14:27