Results 1 to 13 of 13
Thread: Strings and delimeters
- 06-04-2009, 09:07 AM #1
Strings and delimeters
HI people,
I am trying to get user input in a table in the form 00:00
with this in mind, the input has to be returned as a string for it to be in that format. Am thinking of using delimeters to seperate the two values and if the user attempts to input int or double an error shoud be displayed.
Anyone with an idea how i can do this?
Your opinions are highly appreciated
Best regards,
manfizy
- 06-04-2009, 10:27 AM #2
You can use a Pattern and a Matcher to achieve this. With the correct regular expression you can make sure you always get the input you want.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 06-04-2009, 11:22 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I suggest to use regular expressions too in this case. It's much much better/safer than delimiters and stuff.
How did you going to have value type after separation, when you displaying the error message?
- 06-04-2009, 12:24 PM #4
Am still a newbie in java and so am just trying things out - trial and error you know..
I tried reading the Pattarn and matcher class but i couldnt understand a thing lol!
All i want is to make sure as the user edits the table, the table can only accept inputs in "00:00" format. Apparently the input is time, and thats why i want it that way.
@Erlanga, i didnt understand your opinion/question please...
Best Regards,
Manfizy
- 06-04-2009, 01:04 PM #5
Hi,
Are you using Java GUI or is it a HTML page?.Because ,if you use swings for user entry then we are having "javax.swing.JFormattedTextField" class in java.This class will allow the user to enter in the required format.Inside this "JFormattedTextField",they have internally used Patterns.
-Regards
RamyaRamya:cool:
- 06-04-2009, 01:05 PM #6
Search for some info on regular expressions. A simple one to use would be ^\d\d:\d\d$ but you could also be clever and restrict it to valid times using a re as well.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 06-04-2009, 01:26 PM #7
@Ramya - am using Netbeans to build my GUI. Already i have a table that is editable and the user can input some data on it. Your idea seems good. With my little knowledge in java i know that if am to use JFormatTextField i have to listen for editing changes or something..
Just shade somelight by a small example that listens to changes and then uses JFormatTextField.
I hope you dont mind my dumbass questions but am just eager to learn to java.
Thanks
- 06-04-2009, 03:13 PM #8
Hi ,
I have taken a sample code from the net and edited according to your need.Just gothru.
This code will accept only number in "00:00" format.Just gothru the highlighted portion and study.
Java Code:import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.text.MaskFormatter; public class SimpleFTF { public static void main(String args[]) throws ParseException { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS)); [B] MaskFormatter mf1 = new MaskFormatter("##:##"); mf1.setPlaceholderCharacter('_'); JFormattedTextField ftf1 = new JFormattedTextField(mf1);[/B] content.add(ftf1); f.setSize(300, 100); f.setVisible(true); } }Ramya:cool:
- 06-08-2009, 09:22 AM #9
Hi, thank you for your reply
This seems to be the solution to my problem but how can i impliment this on a table such that this happens only when the cells in the table are being edited??
- 06-08-2009, 09:53 AM #10
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
You could of course, use the String.split function.
I die a little on the inside...
Every time I get shot.
- 06-09-2009, 07:42 AM #11
I did alil bit of research on how i can implement the maskFormatter method in a table and this is what i could come up with. BUT i still get some errors.
Could you please tell me exactly where i am making a mistake in my code
Apparently the formatting should only be in the first column cells. The rest should remain the way they are.Java Code:public void focusGained(FocusEvent e) { try{ int rows = table1.getRowCount(); MaskFormatter mf1 = new MaskFormatter("##:##"); mf1.setPlaceholderCharacter(' '); JFormattedTextField ftf1 = new JFormattedTextField(mf1); table1.add(ftf1); for (int i =0;i<rows;i++){ table.setValueAt(ftf1 , i, 0); } }catch (NumberFormatException nfe){ System.out.println("invalid input"); } } public void focusLost(FocusEvent e) { focusGained(e); }
Best Regards,
Manfizy
- 06-09-2009, 07:47 AM #12
I did a lil bit of research on how i can implement the masFormatter method in a JTable and this is what i came up with. BUT i still get some errors in my code.
Couls you please tell me exactly where i am making a mistake in my code??
Apparently the formatting should be only in the first column cells and the rest should remain the way they are.Java Code:public void focusGained(FocusEvent e) { try{ int rows = table1.getRowCount(); MaskFormatter mf1 = new MaskFormatter("##:##"); mf1.setPlaceholderCharacter(' '); JFormattedTextField ftf1 = new JFormattedTextField(mf1); table1.add(ftf1); for (int i =0;i<rows;i++){ table1.setValueAt(ftf1 , i, 0); } }catch (NumberFormatException nfe){ System.out.println("invalid input"); } } public void focusLost(FocusEvent e) { focusGained(e); }
Best Regards,
Manfizy
- 06-09-2009, 07:51 AM #13
I did a research on how i can implement masFormatter method in Jtable and came up with the code below THOUGH i still get some errors in my code.
could you please tell me where i could be making a mistake in my code.
Apparently the formatting should only be in the first column cells and the rest should remain the same.Java Code:public void focusGained(FocusEvent e) { try{ int rows = table1.getRowCount(); MaskFormatter mf1 = new MaskFormatter("##:##"); mf1.setPlaceholderCharacter(' '); JFormattedTextField ftf1 = new JFormattedTextField(mf1); table1.add(ftf1); for (int i =0;i<rows;i++){ table1.setValueAt(ftf1 , i, 0); } }catch (NumberFormatException nfe){ System.out.println("invalid input"); } } public void focusLost(FocusEvent e) { focusGained(e); }
Best Regards
manfizy:confused:
Similar Threads
-
The java Strings
By greenteachacha in forum Jobs DiscussionReplies: 8Last Post: 04-16-2009, 05:08 AM -
comparing strings
By diggitydoggz in forum New To JavaReplies: 7Last Post: 12-23-2008, 04:40 AM -
Comparing Strings
By souFrag in forum Advanced JavaReplies: 5Last Post: 05-21-2008, 09:03 AM -
characters + strings
By Gilgamesh in forum New To JavaReplies: 3Last Post: 03-02-2008, 09:10 PM -
Comparison of Strings
By Cero.Uno in forum New To JavaReplies: 3Last Post: 02-11-2008, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks