Bookmark Topic Watch Topic
  • New Topic

Confused...  RSS feed

 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Report post to moderator
I can't find what's wrong in this program...can someone help me? Thanks.
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.
It says the errors are coming from this part..
prompt = new JLabel("Enter a sentence and press
enter");
c.add (prompt);
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
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Report post to moderator
C:\Documents and Settings\Kels\kelsey>javac TokenTest.java
TokenTest.java:20: unclosed string literal
prompt = new JLabel("Enter a sentence and press
^
TokenTest.java:21: unclosed string literal
enter")
^
TokenTest.java:22: ')' expected
c.add (prompt);
^
3 errors

Here are the errors...from the compiler.
 
maneesh subherwal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Report post to moderator
it seems like the word enter is on the next line... the code should close the JLabel on the same line...
eg...
new JLabel("Enter something and press Enter"); // all on one line...
hope this helps...
Maneesh
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Report post to moderator
Okay that worked, it ran, but it did not show the reverse order of the string...this instead came up on the compiler!! HELP!

C:\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)
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think the problem is here:

I believe that you want to start your "for" loop at "tokens.length-1", which is the last array element.
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Report post to moderator
Okay, I changed that...and I still had the error. It will not display the reversed order...
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Report post to moderator
Kelsey
One reason you might be confused is that you posted the same question in two different places.
Please try to keep this discussion to this thread, so that all of the answers will be in one place.
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Report post to moderator
I understand that...I still cannot get this program to work correctly...
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Report post to moderator
As Dave has decreed, so shall it be done. Please post followups in the other thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!