Substring in java

hi how can i do the following substring java
IF substr(tuser,1,1) = 'N' THEN
   IF NVL(length(tuser), 0)!= 14 THEN 
      message ('social security must be 13 characters long');
      raise form_trigger_failure;
   END IF;
END IF;
IF substr(tsuser,1,1) = 'K' THEN
   IF substr(Tuser,10,1) != ' ' THEN
      message ('The socia Number must be 8 characters long');
      raise form_trigger_failure;
  END IF;
END IF;

Open in new window


i what something like this
if (str.length() >= 4) {
  a = str.substring(0, 4);
}
else {
  // whatever you want to do when length is < 4
}
chalie001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
I find the online documentation to be very helpful in situations like this.

http://docs.oracle.com/javase/7/docs/api/
public String substring(int beginIndex,
               int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:

 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"
slightwv (䄆 Netminder) Commented:
For the starting character check, there is a different call for substring if you want to use that but you could still use the one above.

public String substring(int beginIndex)

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string
CEHJCommented:
Your code is confusing

tuser
tsuser
Tuser

Open in new window


a. How many different variables are there in the above?
b. surely it's not only the length of the string but the type of characters that are in it that are important?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Personally, I wouldn't even use substring. You are only ever pulling out 1 character, so in Java just use the .charAt() method, such as...

if (tuser.charAt(0) == 'N' && tuser.length() != 14) {
    // Show message that SSN must be 13 characters long
} else if (tuser.charAt(0) == 'K' && tuser.charAt(9) != ' ') {
    // Show message that socia number must be 8 characters long
}

Open in new window

chalie001Author Commented:
i have the following but is not working,when my page load

<af:inputText value="#{bindings.SignId.inputValue}"
                                label="#{bindings.SignId.hints.label}"
                                columns="#{bindings.SignId.hints.displayWidth}"
                                maximumLength="#{bindings.SignId.hints.precision}"
                                shortDesc="#{bindings.SignId.hints.tooltip}"
                                id="it3" validator="#{SecValidation.UsrName}"
                                required="true" autoSubmit="true"
                                immediate="true">
                    <f:validator binding="#{bindings.SignId.validator}"/>
                  </af:inputText>
                         
                         
                         
                         
 
				  public class ValidationUsr {
    public ValidationUsr() {
    }

    public void UsrName(FacesContext facesContext, UIComponent uIComponent,
                        Object object) {
        // Add event code here...
        String msg = "The ID Number must be 13 characters long";
        String msg1 = "The Force Number must be 8 characters long";
        
        
        
        if (object.toString().substring(1, 1) == "N")
        {
        if(object.toString().length() != 14)
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,msg,null));
        } 
        //else
        //{
            if(object.toString().substring(1, 1) == "L")
            {
                if(object.toString().length() != 8)
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,msg1,null));  
                    
                }
            }
        
}

Open in new window

my page flow scope is request
CEHJCommented:
You will almost certainly find it easiest to use String.matches. I can't say how as you haven't answered my questions or provided any details about the nature of digits v. alpha
chalie001Author Commented:
THE NATURE of digit
L12345678
or
N8211245664089
CEHJCommented:
boolean valid = s.matches("L\\d{8}|N\\d{13}");

Open in new window


(but don't use the literal)
chalie001Author Commented:
where is s define in s.matches
CEHJCommented:
's' is whatever string needs to be validated
chalie001Author Commented:
ok i managed to have this is working but i wht to display mdifiirent message
 
 public void validUsr(FacesContext facesContext, UIComponent uIComponent, Object object){
        String name=object.toString();  
        String expression= "L\\d{8}|N\\d{13}";
        CharSequence inputStr=name;
        Pattern pattern=Pattern.compile(expression);
        Matcher matcher=pattern.matcher(inputStr);
        String msg="The Force Number must be 8 characters long"; 
        if(matcher.matches()){                   
        }
        else{
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,msg,null));
        
    }
    }

Open in new window


how can i put the if statement
CEHJCommented:
You don't need to use Pattern. Give the an error message specifying the formats for both types of input. And you should probably be using FacesMessage, not hard-coded strings
mccarlIT Business Systems Analyst / Software DeveloperCommented:
    public void validUsr(FacesContext facesContext, UIComponent uIComponent, Object object){
        String inputString=object.toString();
        if (inputString.charAt(0) == 'L') {
            if (!inputString.matches("L\\d{8}") {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Must be 8 digits",null));
            }
        } else if (inputString.charAt(0) == 'N') {
            if (!inputString.matches("N\\d{13}") {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Must be 13 digits",null));
            }
        } else {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"First character must be L or N",null));
        }
    }

Open in new window


But as CEHJ said, don't hard code the messages (I just did that to show what each condition was checking for)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chalie001Author Commented:
thanks i works
chalie001Author Commented:
correct
chalie001Author Commented:
how can check the null value
 if (inputString == null) {
        } else {
               throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Field must be entered",null));
           }

this code validate when i enter null
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Just remove the second line (the "else" line) and it should work.

By the way, you probably should have assigned some points to CEHJ's comments too as they contributed to the final solution.
chalie001Author Commented:
does not work if i enter null than it work
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.