File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer" Watch "StringTokenizer" New topic
Author

StringTokenizer

Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
I have a problem with my program.
I am supposed to write an application that inputs a line of text, tokenizes the line with an object of class StringTokenizer and outputs the tokens in reverse order. Use space characters as delimiters.
my compiler freaks out saying...

:\Documents and Settings\Kels\kelsey>java TokenTest
java.lang.ArrayIndexOutOfBoundsException: 6
at TokenTest$1.actionPerformed(TokenTest.java:43)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:491)
at javax.swing.JTextField.postActionEvent(JTextField.java:672)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:7
86)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1530)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2438)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2473)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2401)
at java.awt.Component.processEvent(Component.java:4909)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja
va:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboard
FocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeybo
ardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo
ardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc
usManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


Here is my program code, please help!
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class TokenTest extends JFrame
{
private JLabel prompt;
private JTextField input;
private JTextArea output;
private String out;
public TokenTest()
{
super (" testing Class StringTokenizer ");
Container c = getContentPane();
c.setLayout(new FlowLayout());
prompt = new JLabel("Enter a sentence and press
enter");
c.add (prompt);
input = new JTextField (30);
input.addActionListener(
new ActionListener()
{
public void actionPerformed (ActionEvent e)
{ String st = e.getActionCommand();
StringTokenizer tokenarr = new
StringTokenizer(st);
StringTokenizer tokens[]= new
StringTokenizer[st.countTokens()];
output.setText("The reverse string is as follows:"
);

// int l= tokens.length;
for (int i = tokens.length;i >= 0; i--)
{
out += tokens[i].nextToken();
}
output.append(out);
output.append("i ma ");
}//end of actionperformed

} //end of new actionlistener
);// end of add actionlistener

c.add(input);
output= new JTextArea(10,20);
output.setEditable(false);
c.add( new JScrollPane(output));
setSize (400,300);
show();
}//end of Tokentest constructor

public static void main (String args[])
{
TokenTest tok = new TokenTest();
tok.addWindowListener(
new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{ System.exit(0);
}
}//end of windowAdapter
);//end of new WindowAdapter
}//end of main method

} // end of Class TokenTest
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This is not the "compiler freaking out," this is a runtime error. In this bit of code

note that the index of the last array element is tokens.length-1, not tokens.length. A 6-element array has indices 0...5. By using tokens.length as an array index, you force an ArrayIndexOutOfBoundsException.


[Jess in Action][AskingGoodQuestions]
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Okay, I did make that change to length-1 and re-compiled the program, and I was given the following errors.
C:\Documents and Settings\Kels\kelsey>javac TokenTest.java
TokenTest.java:1: 'class' or 'interface' expected
> import javax.swing.*;
^
TokenTest.java:2: 'class' or 'interface' expected
> import java.util.*;
^
TokenTest.java:3: 'class' or 'interface' expected
> import java.awt.event.*;
^
TokenTest.java:4: 'class' or 'interface' expected
> import java.awt.*;
^
TokenTest.java:5: 'class' or 'interface' expected
>
^
TokenTest.java:7: '{' expected
> {
^
TokenTest.java:20: unclosed string literal
> prompt = new JLabel("Enter a sentence and press
^
TokenTest.java:21: unclosed string literal
> enter");
^
TokenTest.java:92: '}' expected
} // end of Class TokenTest
^
9 errors
I am new to this and I do not know how to correct these at all. Can someone help me so that my program can run properly? Please.
Thank you. I really appreciate it.
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Now I fixed, or seemed to fix all the other problems except this error ran on the compiler for my code. What should I do to fix this? And to make it run finally?
C:\Documents and Settings\Kels\kelsey>javac TokenTest.java
TokenTest.java:50: cannot resolve symbol
symbol : method countTokens ()
location: class java.lang.String
StringTokenizer[st.countTokens()];
^
1 error
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


String st = e.getActionCommand();
StringTokenizer tokenarr = new
StringTokenizer(st);
StringTokenizer tokens[]= new
StringTokenizer[st.countTokens()];

In the first line above, "st" is declared to be a String; in the last line, you're calling countTokens() on it. String has no such method; mayeb you mean tokenarr.countTokens()?
Why are you making an array of StringTokenizets here, anyway?
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Yes, I did correct the tokenarr part of that problem. It did compile fine without any errors. The box appeared with the "Enter the sentence" for the user to type in a string, then when pressing enter the text area which should have shown the reverse order of that string that was entered did not show up. Instead, the compiler showed this message.
What do I do now, so that the reverse order of the string will show as output??
C:\Documents and Settings\Kels\kelsey>java TokenTest
java.lang.NullPointerException
at TokenTest$1.actionPerformed(TokenTest.java:61)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:491)
at javax.swing.JTextField.postActionEvent(JTextField.java:672)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:7
86)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1530)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2438)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2473)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2401)
at java.awt.Component.processEvent(Component.java:4909)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja
va:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboard
FocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeybo
ardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo
ardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc
usManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, look at the stack trace. It tells you there was a NullPointerException at line 61 in the actionPerformed method. Look at that line. It's this one, I bet:
output.setText("The reverse string is as follows:");
So, how could this throw a NullPointerException? Only if "output" had never been assigned. Why, bless my stars, it never has been! So to fix this error, you'll need to actually create a widget for "output" and add it to the container so it appears on the screen. Then you can recompile and try again.
Almost certainly, you'll get another crash. If you do, please try this same process yourself next time -- it's the only way you're going to learn. Read the stack trace, find the line where the error occurred, think about what could be going wrong, test your hypothesis, and fix the code if needed.
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
What are you feeding the StringTokenizer?
I don't see any getText() method calls to get the value of the textfield. Get the String from the textfield and then feed it to the tokenizer. Make sure it is not null or empty.
[ December 01, 2003: Message edited by: Murali Nanchala ]

"It is almost always answered in the API" - Murali Nanchala
Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
Can someone show me how to write that though? I don't know what both of you are saying...I cannot seem to figure that out, like how to write it. I'm sorry...
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
Neverming my babble about the getText() method. What you had in your code works just fine. I cleaned up the program, left your original lines in there. So you know what was happening.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StringTokenizer