File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong?!?

 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • 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.

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
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you mean tokenarr.countTokens()
public void actionPerformed (ActionEvent e)
{ String st = e.getActionCommand();
StringTokenizer tokenarr = new
StringTokenizer(st);
StringTokenizer tokens[]= new
StringTokenizer[st.countTokens()]; // st is String object
output.setText("The reverse string is as follows:"
);
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I don't know where to fix that....is there a typo error in my program? I'm relatively new to this and I am still a bit confused...
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says my errors come from this part...

prompt = new JLabel("Enter a sentence and press
enter")
c.add (prompt);

?? Please help!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks OK to me, unless that line break inside the string is real -- quoted strings can't include line breaks that way (you can use the two characters
"\n" to mean a line break if needed (although JLabels can't display multiple lines!))
When posting a question to ask about compiler messages, it helps to actually cut and paste the errors into your post.
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • 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.
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to change the two lines statement into one line, compiler should be OK
ie, change

to

 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay it ran, I could enter the string but when I pressed enter to show the string in the reverse order....this came up on the compiler!!!
HELP! I just need the reverse order to then print...in the text area...
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)
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelsey
If you are getting a compiler error there it is most liekly becasue of the formatting of the code. Does the code on your screen look like the code you have posted? Is there a line break after the word 'press'? If so then you need to fix that, along with all of the other ones on the page too.
Now, about your code. You need to step back and think of what you are trying to do. Did you write down the steps you're going to take to accomplish this assignment? If not then take a few minutes to do so....
OK, the first couple of steps should look something like this:
Get input
break input into tokens with StringTokenizer
Now what? You have a StringTokenizer with all of the parts of the sentence in it. Is there anything in the StringTokenizer API that'll help with the next step? What is the next step?
Once you have all of the steps down you can decide how to go about implementing them. It looks like you are on the right track and maybe writing them down will let you see what it is you're doing wrong.
That's it for my lesson in an organized approach to coding.
Here are a couple of hints for you:
--The API page has a good example of how to go about doing one of the things you are going to need to do. Check out the example that uses the hasMoreTokens and nextToken methods.
-- remember that the length of an array is going to be one more than the last index actually held in the array.
hope some of that helps
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelsey,
please pay attention to this note & compare the range of i in your for loop, it should fix the ArrayIndexOutOfBoundary Exception


remember that the length of an array is going to be one more than the last index actually held in the array.
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the status now ?
are you getting any output ? how it looks like ?
 
Kelsey kelskjs
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still cannot get this program to work correctly...
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the output result?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic