Welcome to Dream.In.Code
Become a Java Expert!

Join 149,585 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,783 people online right now. Registration is fast and FREE... Join Now!




Input Format (Input Mask)

 
Reply to this topicStart new topic

Input Format (Input Mask)

Mike007
15 Oct, 2007 - 12:03 PM
Post #1

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 248



Thanked: 4 times
Dream Kudos: 75
My Contributions
What i need is a way to get input from the user, using what Microsoft likes to call a Input Mask, like you can set for Access databases. I would love to learn how to do that for GUI controllers, but what i really need is to learn how to do it with the termminal window. I use the Scanner class to get the user input here is what i want to do, user enters a number, i want it to be a 9 digit number (only number of exactly 9 digits would be accepted) and to be displayed like that:
xxx xxx xxx
123 456 789

not like that:
123456789

Kinda like when you enter a cd key.


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Input Format (Input Mask)
15 Oct, 2007 - 01:31 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Have you taken a look at the MaskFormatter object? Here is an example of how it works...

CODE

// Include our formatter in the javax package and the parse exception error object
import javax.swing.text.*;
import java.text.ParseException;

public class phonemask {
    public static void main(String args[]) {

        try {
            // Set the mask format
            MaskFormatter f = new MaskFormatter("### ###-####");

            // Tell it to use literals as well in the mask (the hyphen in our case)
            f.setValueContainsLiteralCharacters(false);

            // The number to then format and put it out as a string
            System.out.println(f.valueToString("1234567890"));
        }
        catch (ParseException p) { System.out.println(p.toString()); }
    }
}


Then all you got to do is make sure that it is the correct length. Enjoy! smile.gif

This post has been edited by Martyr2: 15 Oct, 2007 - 01:34 PM
User is offlineProfile CardPM
+Quote Post

Mike007
RE: Input Format (Input Mask)
15 Oct, 2007 - 05:13 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 248



Thanked: 4 times
Dream Kudos: 75
My Contributions
First of all thanks for the quick reply, but i was kinda looking for a way to input it like that not output. For output i can also use the DecimalFormatter class.
umm on second thought, can i also use it for inputing data(MaskFormatter)?

If i was creating a GUI program i would use 3 textboxes with the length of 3 and numbers only type, or something like that. Now i want to do something similar just in a terminal window, if you used Input Masks in Access before you would know what i mean.

This post has been edited by Mike007: 15 Oct, 2007 - 05:23 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Input Format (Input Mask)
15 Oct, 2007 - 09:29 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
No, you can't force the mask on the what the person inputs, just after they input you can format it to a certain format. That is because you don't raise events in a console program like a gui control which could evaluate what was entered and throw the value away if it wasn't correct (assuming that is what you are talking about).


User is offlineProfile CardPM
+Quote Post

Mike007
RE: Input Format (Input Mask)
16 Oct, 2007 - 01:32 PM
Post #5

D.I.C Head
Group Icon

Joined: 30 Aug, 2007
Posts: 248



Thanked: 4 times
Dream Kudos: 75
My Contributions
QUOTE(Martyr2 @ 15 Oct, 2007 - 10:29 PM) *

No, you can't force the mask on the what the person inputs, just after they input you can format it to a certain format. That is because you don't raise events in a console program like a gui control which could evaluate what was entered and throw the value away if it wasn't correct (assuming that is what you are talking about).


You are right, i thought that might be impossible. Ok so how do i use that format object to get the actual value, the number value, from a string.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month