Results 1 to 8 of 8
- 12-22-2008, 09:04 PM #1
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
[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(); } }
Java Code:StringTokenizer stok = new StringTokenizer(input);
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 keffie91Last edited by keffie91; 12-23-2008 at 06:48 PM.
Never give up! ;)
- 12-23-2008, 01:19 AM #2
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.
- 12-23-2008, 03:37 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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();
Java Code:JTextField input = new JTextField(); StringTokenizer stok = new StringTokenizer(input.getText());
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?
- 12-23-2008, 06:46 PM #4
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
I want to seperate the operator and the numbers and do a calculation.
Thanks
keffie91Never give up! ;)
- 12-24-2008, 03:49 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
What operator you want to use for separation. How looks like your input string? Did you read that Steve11235s' last post?
- 12-24-2008, 04:56 PM #6
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
the operator can be: * \ + -
the input strong looks like : number1 operator number2
And yes i have read Steve11235s' post.Never give up! ;)
- 12-24-2008, 05:59 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
So you have the solution with you right?
- 12-24-2008, 06:09 PM #8
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
Similar Threads
-
StringTokenizer in a Palindrome program
By jeremyk in forum New To JavaReplies: 10Last Post: 02-13-2010, 07:35 PM -
Help with StringTokenizer!
By ookie833 in forum New To JavaReplies: 13Last Post: 12-14-2008, 05:09 PM -
StringTokenizer
By carderne in forum New To JavaReplies: 1Last Post: 01-26-2008, 09:19 PM -
StringTokenizer
By Java Tip in forum Java TipReplies: 0Last Post: 11-08-2007, 09:48 AM -
StringTokenizer
By Java Tip in forum Java TipReplies: 0Last Post: 11-03-2007, 10:24 PM
Bookmarks