简体   繁体   中英

Stringtokenizer undefined

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM