Calculate occurrence of given letter in sentence in Java [duplicate]
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?
java
marked as duplicate by Nicholas K, Mark Rotteveel
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.
add a comment |
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?
java
marked as duplicate by Nicholas K, Mark Rotteveel
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 throughsentence
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 inletter
, butletter
should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it withsentence
in your code and then checksentence.charAt(i) == letter.charAt(0)
– Lonely Neuron
Nov 21 '18 at 12:59
add a comment |
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?
java
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
java
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
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
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 throughsentence
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 inletter
, butletter
should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it withsentence
in your code and then checksentence.charAt(i) == letter.charAt(0)
– Lonely Neuron
Nov 21 '18 at 12:59
add a comment |
1
Sure, but it's==
, not=
and you may want to cycle throughsentence
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 inletter
, butletter
should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it withsentence
in your code and then checksentence.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
add a comment |
4 Answers
4
active
oldest
votes
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'.
add a comment |
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.
add a comment |
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.
add a comment |
You can use Guava Lib to perform this operation faster without iterating string.
CharMatcher.is('e').countIn("Write a letter");
Will return 3
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
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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'.
add a comment |
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'.
add a comment |
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'.
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'.
answered Nov 21 '18 at 10:46
user43648user43648
1349
1349
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 10:44
SandSand
1,431319
1,431319
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 10:49
deHaardeHaar
2,36431528
2,36431528
add a comment |
add a comment |
You can use Guava Lib to perform this operation faster without iterating string.
CharMatcher.is('e').countIn("Write a letter");
Will return 3
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
add a comment |
You can use Guava Lib to perform this operation faster without iterating string.
CharMatcher.is('e').countIn("Write a letter");
Will return 3
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
add a comment |
You can use Guava Lib to perform this operation faster without iterating string.
CharMatcher.is('e').countIn("Write a letter");
Will return 3
You can use Guava Lib to perform this operation faster without iterating string.
CharMatcher.is('e').countIn("Write a letter");
Will return 3
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
add a comment |
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
add a comment |
1
Sure, but it's
==
, not=
and you may want to cycle throughsentence
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
, butletter
should only contain one letter, right? So iterating over its seems like a mistake to me. I think you should replace it withsentence
in your code and then checksentence.charAt(i) == letter.charAt(0)
– Lonely Neuron
Nov 21 '18 at 12:59