Hi Everyone,

So I just finished writing a program from my Java class,except I can't figure out how to validate the bookID.

"In the Book class, you need to provide a validate method that validate weather the book id is correct. The book id should be a string, which contains no more than 6 characters, and one digit."

Could someone please help me out with this one? Prehaps an example?

Recommended Answers

All 19 Replies

string, which contains no more than 6 characters, and one digit.

How would you do this manually?
Do it in simple steps. As simple as possible.
For example, in a loop you would:
Get the next character
then do some tests and count

Look at the Character class for useful methods.
Look at the String class for useful methods.

Can use


to count the characters in the String.

first check on the length using .length() function .
if length == 6
then you can loop in your String using for loop for example .
make variable counter count the character of the String if this count after the loop = 5 return true; else return false

for (int i = 0; i < string.length(); i++)

Your logic has some holes in it. Where is your test for a digit?

if this count after the loop = 5 return true

the length is 6 five are character and the six is .....

u r right , i pus assumption String is std chars and numbers only

you also have to check if the six char is number or not ,

by if (string.charAt(i)<='0'&&string.charAt(i)<='9') must be added .

thanks @NormR1

one good solution is you have count = 0;
if you find std char count++

if you find number count --;

else return false ;

if count == 5 return true ;

@Ahmed, this last you posted is a little wrong, I think. because, assume the string is a valid one, then according to the way you described in this last post, the count variable should be 4 at the end, not five (if there are six characters, and five of them are letters, count would be five for counting letters, and subtract one for the number in the string, you get four)

however, I believe that using regular expressions are what are used for string validation most of the time, but this way you one learns using java classes, not so much thinking as a programmer.

no more than 6 characters, and one digit

The original specs.

Assuming a character is in the range: a-z and A-Z

Looks like "A1" is valid. <= 6 chars and 1 digit
Also "ABCDEF2" <= 6 digits and 1 digit

Back to the drawing board for everyone.

@bibiki the counter will count to six not to five so at the end it will be five if there five characters .

about regular expression you are write it will be helpful .
[a-zA-Z]{5}\d but this force the String to contain five characters at first and one number at last , can you modify it insure that the number can be in any place .

boolean isChar(char c) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            return true;
        return false;

    boolean isNum(char c) {
        if ((c >= '0' && c <= '9')) {
            return true;
        return false;

    boolean validate(String string) {

        if (string.length() <= 6) {
             int cntChar = 0, cntNum = 0;
            for (int i = 0; i < string.length(); i++) {
                if (isChar(string.charAt(i)))
                else if (isNum(string.charAt(i)))
            if (cntNum == 1 && cntChar == 5)
                return true;
        return false;

about regular expression you are right * (note it is not regular expression :D)

You're missing the point.
What does "no more than 6" mean?

"1" has no more than 6 char and 1 digit.

I think this is the answer

int cnt=0;
          int i=0;
          int j=0;
          String str="ad8awq";
                 System.out.println("not ok");
                 else if(str.length()<=6&j!=1)System.out.println("not ok");

I think this is the answer

Answer to what?
Did you actually test whether this meets the requirement in the original post?
Do you think this is clear and well-written code (and look at AhmedGhazey's solution before you answer that)?
Do you think you are helping the OP by giving him a complete solution (even if it works, which yours doesn't) to copy/paste into his homework?

boolean isChar(char c) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            return true;
        return false;
boolean isChar(char c) {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            return true;
        return false;

Why do people always do that?

boolean isChar(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

If my one is not working it will not a homework help:-O

I am sorry

Is it should contain only one digit I mean if it contain more than one digit is wrong

If my one is not working it will not a homework help:-O


If you know the exact position of your number in the String, you can use the String.substring method, to decrypt your number. However, you do your testing as you wish! :)

String bookID = "ABCDE5";
int number = 10;
NumberFormatException e = null; // the exception variable that is thrown by the try-catch, you declare it as null
// here you test whether 
	number = Integer.parseInt(bookID.substring(bookID.length() - 1, bookID.length())); // you try to parse the LAST character as an integer
} catch (NumberFormatException e1) {
	e = e1; //if that didn't work, it throws an exception
if (
	bookID.length() == 6
	&& e == null
	&& number <10
{ // if the exception does NOT null and the string is 6 characters long
	System.out.println("Valid ID: " + number);

	//you can do the testingg with regular expressions also

} else {
	System.out.println("Not a valid ID..."); 
Be a part of the DaniWeb community

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