English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I tried to do this in my class:

public void replaceWord(String app, String rep)
{
while (input.hasNext())
{
String word = input.next();
System.out.println(word);
}
}

but why doesn't it work??

I tried it with:


public int getNumberOfWords()
{
int n = 0;
while (input.hasNext())
{
String word = input.next();
n++;
}
return n;
}

and it works.

My purpose here is to print out every single word on my input

2006-10-27 04:18:52 · 2 answers · asked by ? 1 in Computers & Internet Programming & Design

input is a scanner

I declared private Scanner input;

at the top

2006-10-27 04:51:55 · update #1

2 answers

I can't understand what your variable 'input' is... what class is it?

However, what you want can be accomplished easily using java.util.StringTokenizer, or simply by using String.split()

For example, suppose you have loaded the text into the variable 'szText'. Use the following code snippet to get all the words:

string[] words = szText.split(" ");

Then to print it, simply loop through the 'words' array and print every element!

2006-10-27 04:47:29 · answer #1 · answered by sushovande 2 · 0 0

This can't be answered because you haven't shown us how you constructed the Scanner.
Did you use the constructor that takes a String object? If so, is it a non-empty, not-all-whitespace string?
Show the rest of your initialization code and maybe I or someone else can help.

Also, describe exactly what result you DO get.

2006-10-30 19:08:22 · answer #2 · answered by vincentgl 5 · 1 0

fedest.com, questions and answers