Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-22-2008, 08:04 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
Rep Power: 0
keffie91 is on a distinguished road
Default [SOLVED] StringTokenizer with JTextField
Hello, i want to write a calculator with buttons and a JTextField. I want to use StringTokenizer to separate the two numbers and the operator.

I have tried this:
Code:
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Calculator extends JFrame {
    StringTokenizer stok = new StringTokenizer(input.getText());
    JTextField input = new JTextField();
    public Calculator(){
        Container c = getContentPane();
        
        
        setTitle("Calculator by keffie91");
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new Calculator();
    }

}
and like this
Code:
StringTokenizer stok = new StringTokenizer(input);
But I get the following error message: illegal forward reference.

I don't know what that mean. Is it even possible to use a JTextField with a StringTokenizer?

I have searched google but notting found.

Thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by keffie91; 12-23-2008 at 05:48 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-23-2008, 12:19 AM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 798
Rep Power: 1
Steve11235 is on a distinguished road
Default
Handling text in this situation get complicated. Here are a couple of suggestions...

1. Use Stirng.replaceAll("regex", ""); to remove anything *except* the characters you actually want (sign, digits, decimal, operator). The regex would look *like* [^\d-\.\+/\*].

2. Use String.split("regex") to "tokenize" the input. The regex would look *like* [-/\+\*]

This will give you a String[] with the pieces. Have fun validating the input.

If you don't know about regular expressions, take the time figure them out. They're very useful, and String has several very important methods that use them.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-23-2008, 02:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Originally Posted by keffie91 View Post
But I get the following error message: illegal forward reference.
That error message means you have use a variable before use.

In your code have something like this.

Code:
    StringTokenizer stok = new StringTokenizer(input.getText());
    JTextField input = new JTextField();
You have initialize the variable input in the second line, that's why you get the error. Interchange that two lines as follows. Compile it, you don't have that error.

Code:
    JTextField input = new JTextField();
    StringTokenizer stok = new StringTokenizer(input.getText());



Originally Posted by keffie91 View Post
Is it even possible to use a JTextField with a StringTokenizer?
Form the JTextField you must get the string first of all. On that string you can apply the StringTokernizer. But I'm not advice to use it, mess on Java. Better to use regular expression.

Can you more clearly explain what you want to do on a string?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-23-2008, 05:46 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
Rep Power: 0
keffie91 is on a distinguished road
Default
I want to seperate the operator and the numbers and do a calculation.

Thanks

keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-24-2008, 02:49 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
What operator you want to use for separation. How looks like your input string? Did you read that Steve11235s' last post?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-24-2008, 03:56 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
Rep Power: 0
keffie91 is on a distinguished road
Default
the operator can be: * \ + -

the input strong looks like : number1 operator number2

And yes i have read Steve11235s' post.
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-24-2008, 04:59 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
So you have the solution with you right?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-24-2008, 05:09 PM
keffie91's Avatar
Member
 
Join Date: Jun 2008
Location: The Netherlands
Posts: 35
Rep Power: 0
keffie91 is on a distinguished road
Default
I have no time to try now. I'm very bizzy. But i think it's enough.

Thanks keffie91
__________________
Never give up!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with StringTokenizer! ookie833 New To Java 13 12-14-2008 04:09 PM
StringTokenizer in a Palindrome program jeremyk New To Java 9 10-02-2008 03:46 AM
StringTokenizer carderne New To Java 1 01-26-2008 08:19 PM
StringTokenizer Java Tip Java Tips 0 11-08-2007 08:48 AM
StringTokenizer Java Tip Java Tips 0 11-03-2007 09:24 PM


All times are GMT +2. The time now is 02:31 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org