I want to write a function to accept a single string that represent a credit card number as parameter and validates it with the format
9999 9999 9999
where 9 is any digit and each quadruplet is separated with a single space.

1. what do you mean 'validates it with'? you mean: formats it to look like?

and ehm ... what is stopping you from doing so?

I don't really know how to do it..

can't help you if you can't even figure that out for yourself...
That said, it's a completely incorrect way to "validate" a credit card number. Not only is the number of digits incorrect for some types of cards, there is a strict requirement for the number to be a valid credit card number which has nothing to do with there being some spaces in it.

i have tried this:

public static boolean validate( String creditNum)
{
  booleann valid=true;
  if (creditNum.lenghth() != 19)
      valid = false;
  else 
  { 
    for ( int i =0; i< creditNum.lenght(), i++)
    c = creditNum.charAt(4) && c = creditNum.charAt(9)&& c = creditNum.charAt(14);
    if( c!= "")
    valid=false;

    elseif (creditNum.charAt(i) < '0' || creditNum.charAt(i) > '9')
    valid =false;
  
  }
  return valid;
}

just some remarks:

c = creditNum.charAt(4) && c = creditNum.charAt(9)&& c = creditNum.charAt(14);
if( c!= "")
valid=false;

you never declare a c variable.
you're overwriting the c value with each charAt(..)

elseif (creditNum.charAt(i) < '0' || creditNum.charAt(i) > '9')

you have the char '0' and the numerical 0, not quite the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.