d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Lab
12Next
Add Reply New Topic New Poll
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Sep 24 2014 10:15pm
Hey guys, it's late and I want to go ZzZz, but I need to end this before I head to bed.

so basically, someone will need to enter a number in a certain format

Code
strNumber=JOptionPane.showInputDialog("Enter the number");


it has to be in this format : 999-999-999 or 999 999 999 or 999/999/999

There has to be 9 numbers and in one of these 3 formats

if it is in one of these formats ... it will just say : Valid , and if it's not , it shall say Non Valid.

Im new with java so that is why I am wondering.

Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 24 2014 11:22pm
Sounds like a job for Regular Expressions.
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Sep 24 2014 11:31pm
It's good , finally found how, now I can go to bed!


I used regex or however it's called.

Here it is .. ( in french )

Code

String sNumero;


//lecture
sNumero=JOptionPane.showInputDialog("Entrer le numéro d'assurance sociale");

Pattern pattern = Pattern.compile("\\d{3}-\\d{3}-\\d{3}");
Matcher matcher = pattern.matcher(sNumero);
Pattern fattern = Pattern.compile("\\d{3} \\d{3} \\d{3}");
Matcher satcher = fattern.matcher(sNumero);
Pattern tattern = Pattern.compile("\\d{3}/\\d{3}/\\d{3}");
Matcher gatcher = tattern.matcher(sNumero);


if (((matcher.matches())||(satcher.matches()))||(gatcher.matches())) {
JOptionPane.showMessageDialog(null, "VALIDE");
}
else
{
JOptionPane.showMessageDialog(null, "NON VALIDE");
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 24 2014 11:38pm
The three can be combined into a single regular expression so you don't have to compile it three times. Although, I will admit, it is a bit late and I am not sure exactly what the performance gains/loss it would be doing it either way would be. Kudos on coming up with the solution though. You will go far.
Member
Posts: 6,604
Joined: May 29 2013
Gold: 2.72
Sep 25 2014 05:47am
Quote (Minkomonster @ Sep 25 2014 01:38am)
The three can be combined into a single regular expression so you don't have to compile it three times. Although, I will admit, it is a bit late and I am not sure exactly what the performance gains/loss it would be doing it either way would be. Kudos on coming up with the solution though. You will go far.


I did not come up with the solution, google came out with it! But thanks anyways :p
Member
Posts: 160
Joined: Sep 25 2014
Gold: 0.01
Sep 25 2014 11:12am
Quote (Jibbosh @ Sep 25 2014 12:47pm)
I did not come up with the solution, google came out with it! But thanks anyways :p


lol

good old google
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 25 2014 04:41pm
Quote (Jibbosh @ Sep 25 2014 07:47am)
I did not come up with the solution, google came out with it! But thanks anyways :p


Well in that case:

Quote (Minkomonster @ Sep 25 2014 01:38am)
The three can be combined into a single regular expression so you don't have to compile it three times. Although, I will admit, it is a bit late and I am not sure exactly what the performance gains/loss it would be doing it either way would be. Kudos on coming up with the solution though. You will go very far.


fix'd
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 25 2014 09:42pm
Quote (carteblanche @ Sep 25 2014 05:41pm)
Well in that case:



fix'd


Google skills are king. We just hired a new "developer" who I had to teach how to add a reference assembly to a project in Visual Studio 4 times and still doesn't get it. She spent 4 hours googling how to open a simple text file. Really? How did she make it through tech screening? Ugh.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 25 2014 09:49pm
Quote (Minkomonster @ Sep 25 2014 11:42pm)
Google skills are king. We just hired a new "developer" who I had to teach how to add a reference assembly to a project in Visual Studio 4 times and still doesn't get it. She spent 4 hours googling how to open a simple text file. Really? How did she make it through tech screening? Ugh.


don't you just go into the solution explorer, right click, add, add reference? or something. it's been a few years and a few versions.



This post was edited by carteblanche on Sep 25 2014 09:51pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 25 2014 09:59pm
Quote (carteblanche @ Sep 25 2014 10:49pm)
don't you just go into the solution explorer, right click, add, add reference? or something. it's been a few years and a few versions.

That's exactly how. And thats how I showed her 4 times. She's just a tool shed.

Quote

http://s.quickmeme.com/img/b2/b28fbb6e2630e16172eb256c347067a53082bccf9fc04558b2a62e9d46798248.jpg


Doubtful. Unless she is also into the 40 year old Indians I work with as well.

This post was edited by Minkomonster on Sep 25 2014 09:59pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll