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.
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.
Re: StringTokenizer error
Quote:
Originally Posted by
Norm
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.
Re: StringTokenizer error
Quote:
StringTokenizer (java.lang.String[], java.lang.string)
Look closely at this. Does this match any of the StringTokenizer constructors
Re: StringTokenizer error
Quote:
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:
Quote:
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
Re: StringTokenizer error
Quote:
Originally Posted by
mjpam
I am having trouble using the StringTokenizer class in following code.
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:
Code :
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, "+");
Re: StringTokenizer error
Do you have a java programming question?
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.
Re: StringTokenizer error
Quote:
do you know what a Document Listener is
Have you looked in the Tutorial or the API doc?
Re: StringTokenizer error
Quote:
Originally Posted by
Norm
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.
:confused:
Re: StringTokenizer error
Quote:
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.
Re: StringTokenizer error
Quote:
Originally Posted by
Norm
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?
Re: StringTokenizer error
Put together an example program with a text field and a Document listener and post it here with your questions.
Re: StringTokenizer error
Code :
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();
}