Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old September 16th, 2002, 02:57 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Check NRIC

how do i do a check to see if the user input of NRIC is in S1234567D format. 1st char has to be either S or F, then follow by 7 digits and last char has to be a alphabet from a to z.

Codes would be helpful. thanks.

pls help asap.

Reply With Quote
  #2  
Old September 16th, 2002, 03:20 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Easiest way is going to be regex.
[CODE]
import java.util.regex.*;

if ( Pattern.matches( "^[SF][0-9]{7}\p{Alpha}$", request.getParameter( "inputString" ) ) ) {
.. do code ..
} else {
.. no match ..
}

If you're going to be doing multiple matches you'll want to compile the pattern first. See http://java.sun.com/j2se/1.4/docs/a...ex/Pattern.html for instructions.

Reply With Quote
  #3  
Old September 16th, 2002, 03:51 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
currently i am using the 1.3.1 api. it does not have regex. so what do i do??

Reply With Quote
  #4  
Old September 16th, 2002, 04:11 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
upgrade

You're going to have to test properties on the string, then.
string.length() == 9
string.substring( 0, 1 ) == "S" || string.substring( 0, 1 ) == "F"
try {
Integer.parseInt( string.substring( 1, 9 ) );
} catch ( NumberFormatException nfe ) {
... these are not the numbers you are looking for, move along ...
}
string.substring( 8, 9 ).toUpperCase() > "A" && string.substring( 8, 9 ).toUpperCase() < "Z"

I'm not sure about that last one. You may need to convert it to a character or something first.

Reply With Quote
  #5  
Old September 17th, 2002, 02:55 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i have tried ur method.
there is smthing wrong with the middle 7 chars of the NRIC.

What i have now is:
it should check the nric is valid.
if it is valid, it prints "Ok".
however, now if the user types in "SdfgewgfW", as you can see the middle 7 chars are not integers. other than printing out "Middle 7 chars are not DIGITS", it prints out "Middle 7 chars are not DIGITS. Ok."

which means it also prints out the statement for valid nric.

Pls help asap...

Reply With Quote
  #6  
Old September 17th, 2002, 04:08 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Code:
boolean valid = false;
try {
  if ( !string.length() == 9 ) {
    throw new Exception( "Invalid Length" );
  }
  if ( !string.substring( 0, 1 ) == "S" ||   !string.substring( 0, 1 ) == "F" ) {
   throw new Exception( "Invalid First Letter" );
  }
  Integer.parseInt( string.substring( 1, 9 ) );
  if ( !string.substring( 8, 9 ).toUpperCase() > "A" && !string.substring( 8, 9 ).toUpperCase() < "Z" ) {
   throw new Exception( "Invalid Last Letter" );
  }
  valid = true;
} catch ( NumberFormatException nfe ) {
  out.print( "Wrong Middle Section" );
} catch ( Exception e ) {
  e.getMessage();
}
if ( valid ) {
  out.print( "welcome" );
}

Reply With Quote
  #7  
Old September 17th, 2002, 05:00 AM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i can't get it still..
and i have already done this, but the middle 7 chars check still doesn't work..

pls suggest..

Codes
___________________________________________


//check NRIC length = 9
if (nric.length()==9)
{
//check NRIC 1st char = uppercase "S" or "F"
if(nric.substring(0,1).equals("S") || nric.substring(0,1).equals("F"))
{
//check NRIC middle 7 chars are digits
try
{
Integer.parseInt(nric.substring(1,8));
}
catch ( NumberFormatException nfe )
{
out.println("Middle 7 chars are not DIGITS.");
}
//check NRIC last char = uppercase "A" to "Z"
if(nric.substring(8,9).equals("A") || nric.substring(8,9).equals("B") || nric.substring(8,9).equals("C") || nric.substring(8,9).equals("D") || nric.substring(8,9).equals("E") || nric.substring(8,9).equals("F") || nric.substring(8,9).equals("G") || nric.substring(8,9).equals("H") || nric.substring(8,9).equals("I") || nric.substring(8,9).equals("J") || nric.substring(8,9).equals("K") || nric.substring(8,9).equals("L") || nric.substring(8,9).equals("M") || nric.substring(8,9).equals("N") || nric.substring(8,9).equals("O") || nric.substring(8,9).equals("P") || nric.substring(8,9).equals("Q") || nric.substring(8,9).equals("R") || nric.substring(8,9).equals("S") || nric.substring(8,9).equals("T") || nric.substring(8,9).equals("U") || nric.substring(8,9).equals("V") || nric.substring(8,9).equals("W") || nric.substring(8,9).equals("X") || nric.substring(8,9).equals("Y") || nric.substring(8,9).equals("Z"))
{
out.println("Ok.");
}
else
{
out.println("NRIC last char should be a UPPERCASE ALPHABET.");
}
}
else
{
out.println("NRIC first char should be UPPERCASE S or F.");
}
}
else
{
out.println("NRIC total length not = 9.");
}
}

____________________________________________________

Reply With Quote
  #8  
Old September 17th, 2002, 10:16 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
You're going to need to move the try/catch block so that it surrounds the whole thing. Otherwise, the exception is thrown, caught, and everything else just proceeds like normal.


try
{
//check NRIC length = 9
if (nric.length()==9)
{
//check NRIC 1st char = uppercase "S" or "F"
if(nric.substring(0,1).equals("S") || nric.substring(0,1).equals("F"))
{
//check NRIC middle 7 chars are digits
Integer.parseInt(nric.substring(1,8));
//check NRIC last char = uppercase "A" to "Z"
if(nric.substring(8,9).equals("A") || nric.substring(8,9).equals("B") || nric.substring(8,9).equals("C") || nric.substring(8,9).equals("D") || nric.substring(8,9).equals("E") || nric.substring(8,9).equals("F") || nric.substring(8,9).equals("G") || nric.substring(8,9).equals("H") || nric.substring(8,9).equals("I") || nric.substring(8,9).equals("J") || nric.substring(8,9).equals("K") || nric.substring(8,9).equals("L") || nric.substring(8,9).equals("M") || nric.substring(8,9).equals("N") || nric.substring(8,9).equals("O") || nric.substring(8,9).equals("P") || nric.substring(8,9).equals("Q") || nric.substring(8,9).equals("R") || nric.substring(8,9).equals("S") || nric.substring(8,9).equals("T") || nric.substring(8,9).equals("U") || nric.substring(8,9).equals("V") || nric.substring(8,9).equals("W") || nric.substring(8,9).equals("X") || nric.substring(8,9).equals("Y") || nric.substring(8,9).equals("Z"))
{
out.println("Ok.");
}
else
{
out.println("NRIC last char should be a UPPERCASE ALPHABET.");
}
}
else
{
out.println("NRIC first char should be UPPERCASE S or F.");
}
}
else
{
out.println("NRIC total length not = 9.");
}
}
}
catch ( NumberFormatException nfe )
{
out.println("Middle 7 chars are not DIGITS.");
}

Reply With Quote
  #9  
Old September 17th, 2002, 07:49 PM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks... it works..
i also need to ask if u know how to check a string to see if it contains salutations such as Mrs, Miss, Mdm, Mr; etc..

pls help if u know..

Reply With Quote
  #10  
Old September 17th, 2002, 08:12 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
The String class has a startsWith() function.

Reply With Quote
  #11  
Old September 17th, 2002, 08:19 PM
peter83 peter83 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 19 peter83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what if the user enters the salutations at the end of the string or in the middle of the string?? is there smthing like a contain method, whereby it checks if the string contains certain word??

Reply With Quote
  #12  
Old September 17th, 2002, 09:41 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
Yup. Bookmark this link. It's your friend.
http://java.sun.com/j2se/1.3/docs/api/
Get familiar with the javadoc, and you'll be able to answer 90% of your own questions.

As for the function you need:
http://java.sun.com/j2se/1.3/docs/a...ng.html#indexOf(java.lang.String)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Check NRIC


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |