Results 1 to 8 of 8
  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    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:
    Java 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
    Java 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
    Last edited by keffie91; 12-23-2008 at 06:48 PM.
    Never give up! ;)

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote 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.

    Java 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.

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



    Quote 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?

  4. #4
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    I want to seperate the operator and the numbers and do a calculation.

    Thanks

    keffie91
    Never give up! ;)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What operator you want to use for separation. How looks like your input string? Did you read that Steve11235s' last post?

  6. #6
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    the operator can be: * \ + -

    the input strong looks like : number1 operator number2

    And yes i have read Steve11235s' post.
    Never give up! ;)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    I have no time to try now. I'm very bizzy. But i think it's enough.

    Thanks keffie91
    Never give up! ;)

Similar Threads

  1. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 07:35 PM
  2. Help with StringTokenizer!
    By ookie833 in forum New To Java
    Replies: 13
    Last Post: 12-14-2008, 05:09 PM
  3. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 09:19 PM
  4. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 09:48 AM
  5. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •