1

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
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Dave Phan
  • 31
  • 4
  • Oh yes thanks for telling me the other error i forgot to list it as well. And yes the compiler says the errors was the constructor one in first place, I don't what constructor suppose to be with this as I remembered my professor said I only have to declare instance since the class is already in java.util package. I made another one with different class name to make it less confused and also imported java.util, the errors still happen. Can you help me with the constructor thing? – Dave Phan Mar 13 '19 at 00:11

1 Answers1

3

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Agreed. It’s never a good idea to name your class the same as any class anywhere under the `java` package. – Bohemian Mar 13 '19 at 00:02
  • I made another class with different name, and also import java.util.*; the error still happens do you know why? – Dave Phan Mar 13 '19 at 00:02
  • @DavePhan You didn't remove the existing class in the current package with the name `StringTokenizer` (and thus you are still **shadowing** it). – Elliott Frisch Mar 13 '19 at 00:07
  • @ElliottFrisch I actually made the 2nd one on a different laptop and i also change the class name on the first one as well. The prior error said to be for the constructor but I have no idea what it is – Dave Phan Mar 13 '19 at 00:17
  • *The prior error said to be for the constructor* What is the new error? – Elliott Frisch Mar 13 '19 at 00:26
  • there's also errors on hasMoreTokens and nextToken as well, it's not new tho. I thought it's linked up to the constructor error so I forgot to mention in the post but @Carcigenicate reminded me that. – Dave Phan Mar 13 '19 at 00:42