Counting words using the Scanner.next() method The Next CEO of Stack OverflowHow do I iterate over the words of a string?How do I read input character-by-character in Java?Count the number occurrences of a character in a stringHow do I replace a character in a string in Java?Does Python have a string 'contains' substring method?How do I check if a string contains a specific word?Scanner problems in JavaScanning for Character error?Beginning Java Method PracticeIn a recursive way, how do I count the number of words in a char array in java?
What does this strange code stamp on my passport mean?
Free fall ellipse or parabola?
Mathematica command that allows it to read my intentions
Why can't we say "I have been having a dog"?
How to find if SQL server backup is encrypted with TDE without restoring the backup
Is it a bad idea to plug the other end of ESD strap to wall ground?
How do I keep Mac Emacs from trapping M-`?
Is there a rule of thumb for determining the amount one should accept for of a settlement offer?
Can a PhD from a non-TU9 German university become a professor in a TU9 university?
logical reads on global temp table, but not on session-level temp table
Does the Idaho Potato Commission associate potato skins with healthy eating?
Compilation of a 2d array and a 1d array
Can Sri Krishna be called 'a person'?
Does Germany produce more waste than the US?
Is a distribution that is normal, but highly skewed, considered Gaussian?
Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?
Incomplete cube
How can I prove that a state of equilibrium is unstable?
Why do we say “un seul M” and not “une seule M” even though M is a “consonne”?
Salesforce opportunity stages
Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?
Is it okay to majorly distort historical facts while writing a fiction story?
What are the unusually-enlarged wing sections on this P-38 Lightning?
Upgrading From a 9 Speed Sora Derailleur?
Counting words using the Scanner.next() method
The Next CEO of Stack OverflowHow do I iterate over the words of a string?How do I read input character-by-character in Java?Count the number occurrences of a character in a stringHow do I replace a character in a string in Java?Does Python have a string 'contains' substring method?How do I check if a string contains a specific word?Scanner problems in JavaScanning for Character error?Beginning Java Method PracticeIn a recursive way, how do I count the number of words in a char array in java?
I am currently working on a method that has to return the number of newline characters, words, and characters of a string in an int[] array. I am confused on how count the number of times the Scanner.next() method runs. I have tried to use an if statement like this:
if (!(in.next() == ("")))
words++;
but I get java.util.NoSuchElementException. How would I get around the NoSuchElementException and count the tokens instead of returning them? Here is what I have so far:
import java.util.Scanner;
public class WordCount {
/**
* Scans a string and returns the # of newline characters, words, and
* characters in an array object.
*
* @param text string to be scanned
* @return # of newline characters, words, and characters
*/
public static int[] analyze(String text)
// Variables declared
Scanner in = new Scanner(text);
int[] values = new int[3];
int line = 0;
int words = 0;
int characters = 0;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if (n == 'n')
line++;
if (in.hasNext())
characters++;
//this is where I think the word count statement should go
values[0] = line;
values[1] = words;
values[2] = characters;
return values;
public static void main(String[] args)
analyze("This isn a test sentence.");
The test should return an array of 1, 5, 25.
java arrays string java.util.scanner
add a comment |
I am currently working on a method that has to return the number of newline characters, words, and characters of a string in an int[] array. I am confused on how count the number of times the Scanner.next() method runs. I have tried to use an if statement like this:
if (!(in.next() == ("")))
words++;
but I get java.util.NoSuchElementException. How would I get around the NoSuchElementException and count the tokens instead of returning them? Here is what I have so far:
import java.util.Scanner;
public class WordCount {
/**
* Scans a string and returns the # of newline characters, words, and
* characters in an array object.
*
* @param text string to be scanned
* @return # of newline characters, words, and characters
*/
public static int[] analyze(String text)
// Variables declared
Scanner in = new Scanner(text);
int[] values = new int[3];
int line = 0;
int words = 0;
int characters = 0;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if (n == 'n')
line++;
if (in.hasNext())
characters++;
//this is where I think the word count statement should go
values[0] = line;
values[1] = words;
values[2] = characters;
return values;
public static void main(String[] args)
analyze("This isn a test sentence.");
The test should return an array of 1, 5, 25.
java arrays string java.util.scanner
This question is being voted down because it is too broad. Take the tour and read How to Ask, and come back later.
– Gendarme
Mar 8 at 19:20
So, you stated what you want your program to do, and you've shown us what you've done so far, but you haven't actually asked a programming question, yet. You haven't shown us what you've tried so far, or given any indication of what you're stuck on. What specific issues are you having writing the word count code?
– azurefrog
Mar 8 at 19:22
I've just updated my post and tried to make it more specific, it's my first time posting so I apologize if I've been too broad or haven't asked the right kind of questions.
– Deven
Mar 8 at 19:37
add a comment |
I am currently working on a method that has to return the number of newline characters, words, and characters of a string in an int[] array. I am confused on how count the number of times the Scanner.next() method runs. I have tried to use an if statement like this:
if (!(in.next() == ("")))
words++;
but I get java.util.NoSuchElementException. How would I get around the NoSuchElementException and count the tokens instead of returning them? Here is what I have so far:
import java.util.Scanner;
public class WordCount {
/**
* Scans a string and returns the # of newline characters, words, and
* characters in an array object.
*
* @param text string to be scanned
* @return # of newline characters, words, and characters
*/
public static int[] analyze(String text)
// Variables declared
Scanner in = new Scanner(text);
int[] values = new int[3];
int line = 0;
int words = 0;
int characters = 0;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if (n == 'n')
line++;
if (in.hasNext())
characters++;
//this is where I think the word count statement should go
values[0] = line;
values[1] = words;
values[2] = characters;
return values;
public static void main(String[] args)
analyze("This isn a test sentence.");
The test should return an array of 1, 5, 25.
java arrays string java.util.scanner
I am currently working on a method that has to return the number of newline characters, words, and characters of a string in an int[] array. I am confused on how count the number of times the Scanner.next() method runs. I have tried to use an if statement like this:
if (!(in.next() == ("")))
words++;
but I get java.util.NoSuchElementException. How would I get around the NoSuchElementException and count the tokens instead of returning them? Here is what I have so far:
import java.util.Scanner;
public class WordCount {
/**
* Scans a string and returns the # of newline characters, words, and
* characters in an array object.
*
* @param text string to be scanned
* @return # of newline characters, words, and characters
*/
public static int[] analyze(String text)
// Variables declared
Scanner in = new Scanner(text);
int[] values = new int[3];
int line = 0;
int words = 0;
int characters = 0;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if (n == 'n')
line++;
if (in.hasNext())
characters++;
//this is where I think the word count statement should go
values[0] = line;
values[1] = words;
values[2] = characters;
return values;
public static void main(String[] args)
analyze("This isn a test sentence.");
The test should return an array of 1, 5, 25.
java arrays string java.util.scanner
java arrays string java.util.scanner
edited Mar 8 at 19:36
Deven
asked Mar 8 at 19:14
Deven Deven
11
11
This question is being voted down because it is too broad. Take the tour and read How to Ask, and come back later.
– Gendarme
Mar 8 at 19:20
So, you stated what you want your program to do, and you've shown us what you've done so far, but you haven't actually asked a programming question, yet. You haven't shown us what you've tried so far, or given any indication of what you're stuck on. What specific issues are you having writing the word count code?
– azurefrog
Mar 8 at 19:22
I've just updated my post and tried to make it more specific, it's my first time posting so I apologize if I've been too broad or haven't asked the right kind of questions.
– Deven
Mar 8 at 19:37
add a comment |
This question is being voted down because it is too broad. Take the tour and read How to Ask, and come back later.
– Gendarme
Mar 8 at 19:20
So, you stated what you want your program to do, and you've shown us what you've done so far, but you haven't actually asked a programming question, yet. You haven't shown us what you've tried so far, or given any indication of what you're stuck on. What specific issues are you having writing the word count code?
– azurefrog
Mar 8 at 19:22
I've just updated my post and tried to make it more specific, it's my first time posting so I apologize if I've been too broad or haven't asked the right kind of questions.
– Deven
Mar 8 at 19:37
This question is being voted down because it is too broad. Take the tour and read How to Ask, and come back later.
– Gendarme
Mar 8 at 19:20
This question is being voted down because it is too broad. Take the tour and read How to Ask, and come back later.
– Gendarme
Mar 8 at 19:20
So, you stated what you want your program to do, and you've shown us what you've done so far, but you haven't actually asked a programming question, yet. You haven't shown us what you've tried so far, or given any indication of what you're stuck on. What specific issues are you having writing the word count code?
– azurefrog
Mar 8 at 19:22
So, you stated what you want your program to do, and you've shown us what you've done so far, but you haven't actually asked a programming question, yet. You haven't shown us what you've tried so far, or given any indication of what you're stuck on. What specific issues are you having writing the word count code?
– azurefrog
Mar 8 at 19:22
I've just updated my post and tried to make it more specific, it's my first time posting so I apologize if I've been too broad or haven't asked the right kind of questions.
– Deven
Mar 8 at 19:37
I've just updated my post and tried to make it more specific, it's my first time posting so I apologize if I've been too broad or haven't asked the right kind of questions.
– Deven
Mar 8 at 19:37
add a comment |
1 Answer
1
active
oldest
votes
For checking the amount of word in a string, you will need to check if the next character is a letter. At the same time, you will need a condition to check if it is end of the word.
boolean isEndWord = false;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if ((!Character.isLetter(n))&&isEndWord == true)
words++;
isEndWord = false;
if (n == ' ')
isEndWord = true;
if (n == 'n')
line++;
isEndWord = true;
if (in.hasNext())
characters++;
You can used the boolean isEndWord to trigger whenever the word ends.
add a comment |
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
);
);
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%2f55069592%2fcounting-words-using-the-scanner-next-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For checking the amount of word in a string, you will need to check if the next character is a letter. At the same time, you will need a condition to check if it is end of the word.
boolean isEndWord = false;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if ((!Character.isLetter(n))&&isEndWord == true)
words++;
isEndWord = false;
if (n == ' ')
isEndWord = true;
if (n == 'n')
line++;
isEndWord = true;
if (in.hasNext())
characters++;
You can used the boolean isEndWord to trigger whenever the word ends.
add a comment |
For checking the amount of word in a string, you will need to check if the next character is a letter. At the same time, you will need a condition to check if it is end of the word.
boolean isEndWord = false;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if ((!Character.isLetter(n))&&isEndWord == true)
words++;
isEndWord = false;
if (n == ' ')
isEndWord = true;
if (n == 'n')
line++;
isEndWord = true;
if (in.hasNext())
characters++;
You can used the boolean isEndWord to trigger whenever the word ends.
add a comment |
For checking the amount of word in a string, you will need to check if the next character is a letter. At the same time, you will need a condition to check if it is end of the word.
boolean isEndWord = false;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if ((!Character.isLetter(n))&&isEndWord == true)
words++;
isEndWord = false;
if (n == ' ')
isEndWord = true;
if (n == 'n')
line++;
isEndWord = true;
if (in.hasNext())
characters++;
You can used the boolean isEndWord to trigger whenever the word ends.
For checking the amount of word in a string, you will need to check if the next character is a letter. At the same time, you will need a condition to check if it is end of the word.
boolean isEndWord = false;
// Checks string for # of newlines, chars, and words
for (int i = 0; i < text.length(); i++)
char n = text.charAt(i);
if ((!Character.isLetter(n))&&isEndWord == true)
words++;
isEndWord = false;
if (n == ' ')
isEndWord = true;
if (n == 'n')
line++;
isEndWord = true;
if (in.hasNext())
characters++;
You can used the boolean isEndWord to trigger whenever the word ends.
answered Mar 8 at 20:20
SemjeromeSemjerome
236
236
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.
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%2f55069592%2fcounting-words-using-the-scanner-next-method%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

This question is being voted down because it is too broad. Take the tour and read How to Ask, and come back later.
– Gendarme
Mar 8 at 19:20
So, you stated what you want your program to do, and you've shown us what you've done so far, but you haven't actually asked a programming question, yet. You haven't shown us what you've tried so far, or given any indication of what you're stuck on. What specific issues are you having writing the word count code?
– azurefrog
Mar 8 at 19:22
I've just updated my post and tried to make it more specific, it's my first time posting so I apologize if I've been too broad or haven't asked the right kind of questions.
– Deven
Mar 8 at 19:37