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

I am asked to write a public method setText that takes a String parameter and changes the current text to this new
String. If no text existed before, this sets it. If there was text, this method will overwrite it. It doesn't return anything. And below is my code:

import java.util.Scanner;

public class WordProcessor
{

private String word;
private Scanner input;

public WordProcessor()
{
word = "";
input = new Scanner(word);
}

public WordProcessor(String inputString)
{
word = inputString;
input = new Scanner(word);
}

public void setText(String set)
{
word = set;
input = new Scanner(set);
}

But if use this method, whenever I try to use the hasNext method to get words on the string, it wont work.. how am I suppose to fix this? Iam confused, thanks

2006-10-27 14:00:46 · 2 answers · asked by ? 1 in Computers & Internet Programming & Design

2 answers

You didn't show the code that is calling hasNext() on the Scanner. Are you perhaps testing this using the default constructor? If so, hasNext() will return false, since the source string will be the empty string "".

2006-10-30 09:22:40 · answer #1 · answered by vincentgl 5 · 0 0

setText
public void setText(String t)Sets the text that is presented by this text component to be the specified text.

Parameters:
t - the new text; if this parameter is null then the text is set to the empty string ""
See Also:
getText()

2006-10-27 14:02:58 · answer #2 · answered by god knows and sees else Yahoo 6 · 0 0

fedest.com, questions and answers