StringTokenizer

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)

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.

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

Posts: 36

posted 14 years ago

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

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?

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)

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.

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

Posts: 36

posted 14 years ago

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

Posts: 74

posted 14 years ago

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 almost always answered in the API" - Murali Nanchala

Always look on the bright side of life. At least this ad is really tiny: