Reputation: 31
Hi i got some problem making a stringtokenizer method which allow string input by user and count and print tokens. Can someone help me solve error? It says the constructor is undefined.
public class StringTokenizer
{
public static void main(String[ ] args)
{
Scanner keyboard;
String inputSentence;
StringTokenizer stok = null; //declare a reference to hold the address of a StringTokenizer object
keyboard = new Scanner(System.in);
System.out.println("Enter a line of text: ");
inputSentence = new String(keyboard.nextLine( ) );
while(!(inputSentence.equals("quit")))
{
stok = new StringTokenizer(inputSentence);//the constructor that take a String and uses a space as the delimeter
while (stok.hasMoreTokens());
{
System.out.println("Number of tokens: " + stok.countTokens( ));
System.out.println(stok.nextToken( ));
}
System.out.println("Enter another line of data or quit\n" );
inputSentence = keyboard.nextLine( );
}
System.out.println("Goodbye");
}//end of main
} //end of class
Upvotes: 1
Views: 572
Reputation: 201399
Rename your class. You are shadowing java.util.StringTokenizer
; alternatively,
java.util.StringTokenizer stok = null;
and
stok = new java.util.StringTokenizer(inputSentence);
But, it'll be less confusing if you rename your class.
Upvotes: 3