Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: StringTokenizer error

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default StringTokenizer error

    I am having trouble using the StringTokenizer class in following code.

    import java.util.*;
     
    class ParseEval
    {
         public static void main (String args[])
              {
                   if (args.length == 0)
                   {
                        System.out.println("No arguments entered");
                   }
                   else if (args.length == 1)
                   {
                        StringTokenizer argsToks = new StringTokenizer(args, "+");
     
                        while(argsToks.hasMoreTokens())
                        {
                             System.out.print(argsToks.nextToken());
                             System.out.println();
                        }
                   }
                   else
                   {
                        System.out.println(args.length);
                   }
              }
    }

    When I try to compile it, I get the following error:

    ParseEval.java:18:cannot find symbol
    symbol: constructor StringTokenizer (java.lang.String[], java.lang.string)
    location: java.util.StringTokenizer
                         StringTokenizer argsToks = new StringTokenizer(args, "+")
                                                                              ^

    I though I had imported the correct packages, but I'm not sure I have.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: StringTokenizer error

    The StringTokenizer constructors do not accept arrays (args). Try just passing the first value of the array, or if needed loop through the array and pass each value to the StringTokenizer.
    Last edited by copeg; July 6th, 2010 at 11:17 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    You need to look at the error message more closely. Especially this line:
    constructor StringTokenizer (java.lang.String[], java.lang.string)
    Then you need to read the API doc for StringTokenizer and check the syntax for its Constructors.

  4. #4
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: StringTokenizer error

    Quote Originally Posted by Norm View Post
    You need to look at the error message more closely. Especially this line:
    constructor StringTokenizer (java.lang.String[], java.lang.string)
    Then you need to read the API doc for StringTokenizer and check the syntax for its Constructors.
    I did read the API doc, and I thought that I was using the StringTokenizer(String str, String delim), which I thought was the correct syntax for what I wanted to do.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    StringTokenizer (java.lang.String[], java.lang.string)
    Look closely at this. Does this match any of the StringTokenizer constructors
    Last edited by Norm; July 14th, 2010 at 06:30 AM. Reason: Wrong class name

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    it is giving error "cannot find symbol" for String Tokenizer.
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol : variable var
    location: class TestSorts
    var = 2;
    ^

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: StringTokenizer error

    Quote Originally Posted by mjpam View Post
    I am having trouble using the StringTokenizer class in following code.

    import java.util.*;
     
    class ParseEval
    {
         public static void main (String args[])
              {
                   if (args.length == 0)
                   {
                        System.out.println("No arguments entered");
                   }
                   else if (args.length == 1)
                   {
                        StringTokenizer argsToks = new StringTokenizer(args, "+");
     
                        while(argsToks.hasMoreTokens())
                        {
                             System.out.print(argsToks.nextToken());
                             System.out.println();
                        }
                   }
                   else
                   {
                        System.out.println(args.length);
                   }
              }
    }

    When I try to compile it, I get the following error:

    ParseEval.java:18:cannot find symbol
    symbol: constructor StringTokenizer (java.lang.String[], java.lang.string)
    location: java.util.StringTokenizer
                         StringTokenizer argsToks = new StringTokenizer(args, "+")
                                                                              ^

    I though I had imported the correct packages, but I'm not sure I have.
    I'm wondering, can a does it have to be ClassName[] referenceVariable = new ClassName[3];
    or can it be ClassName referenceVariable[] = new ClassName[3] ?

    If it does have to do, then does it have to be

    String[] args or is String args[] fine?

    Also, was args ever set for anything because it'll throw a NullPointerException if it hasn't been with the statement StringTokenizer argsToks = new StringTokenizer(args, "+");

    Also, args is an array.

    Wait, I see the problem....

    The constructors for StringTokenizer are:

    StringTokenizer(String str)

    StringTokenizer(String str, String delim)

    StringTokenizer(String str, String delim, boolean returnDelims)

    args is an array.

    The constructor StringTokenizer(String[] args, String str) isn't valid.

    Maybe args[0] as that's an element.

    You could try

    String str = args[0].toString(); // that'll work for sure, especially if args[0] is some value other than a String

    maybe just

    String str = args[0];

    will work.



    StringTokenizer argsToks = new StringTokenizer(str, "+");

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    Do you have a java programming question?

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: StringTokenizer error

    Well, I was trying to answer the question of what might be causing the problem. Also, do you know what a Document Listener is? I need one to be able to get it to add a listener, or at least use the setSelected() method on an item in a JComboBox which itself has an Item Listener, based on what is in the text field.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    do you know what a Document Listener is
    Have you looked in the Tutorial or the API doc?

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: StringTokenizer error

    Quote Originally Posted by Norm View Post
    Have you looked in the Tutorial or the API doc?
    I kinda did, but not much. However, I cannot get an addDocumentListener() method for a JTextField or a JTextArea or even a JFrame.


  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    I cannot get an addDocumentListener() method for a JTextField
    That's correct there isn't one.
    In the API doc page at the top of the page is a link: Index click that. On the index page, click on the first letter of the method here 'a', scroll down or find the method(s) then follow the link to the class with that method.

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: StringTokenizer error

    Quote Originally Posted by Norm View Post
    That's correct there isn't one.
    In the API doc page at the top of the page is a link: Index click that. On the index page, click on the first letter of the method here 'a', scroll down or find the method(s) then follow the link to the class with that method.
    I don't see how this would work with a JTextField. Also, couldn't I just add Key Listeners?

    Also, there is an addKeyListener() method for JTextField.

    Also, couldn't I use fireActionPerformed() to activate the Item Listener?

    However, I'll create one of them Document thingies to see what it'll do.

    How do you get it to activate a Key Listener if the user types in say 12 or makes a capital letter, say L(Shift + l)
    , or ! (Shift + 1), or enters 124 or something or types Bob or something like that?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: StringTokenizer error

    Put together an example program with a text field and a Document listener and post it here with your questions.

  15. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: StringTokenizer error

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
     
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    import javax.swing.event.PopupMenuEvent;
    import javax.swing.event.PopupMenuListener;
    import javax.swing.text.DefaultStyledDocument;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import java.util.*;
    import java.io.*;
    import javax.swing.ImageIcon;
     
     
    import java.awt.event.WindowEvent;
    import java.awt.Window;
    import javax.swing.JScrollPane;
    import java.awt.Component;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
     
    public class thisClass extends JFrame
    {
     
    JTextField cornField, beanField;
    JFrame frame;
    DefaultStyledDocument doc;
     
    public thisClass()
    {
    frame = new JFrame("This is the JFrame I'm using.");
    doc = new DefaultStyledDocument();
     
    cornField = new JTextField(3);
    beanField = new JTextField(3);
     
    frame.add(cornField);
    frame.add(beanField);
     
    frame.setVisible(true);
    cornField.setVisible(true);
    beanField.setVisible(true);
     
    doc.addDocumentListener(
    new DocumentListener() {
    public void changedUpdate(DocumentEvent e)
    {
     
    }
     
    public void insertUpdate(DocumentEvent e)
    {
     
    }
     
    public void removeUpdate(DocumentEvent e)
    {
     
    }
     
     
    }
    );
    }
     
    public static void main(String[] args)
    {
    thisClass refVar = new thisClass();
     
    }