5 Replies - 713 Views - Last Post: 15 March 2010 - 01:32 PM Rate Topic: -----

#1 davidsouth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 10

Basic String Formatting

Posted 15 March 2010 - 12:39 PM

I'm currently writing a library of books program. In my Books class I have a constructor to create a book object with title/author/ect, as you would expect. Book objects also require a reference which I am setting using the method below.

My problem is I want the string ref, which will be my book objects reference to only be in the format RAxxx RBxxx RCxxx where the xxx's are numbers. I want to have this method throwing an exception when an incorrect format is entered. I've been searching for information on string formatting, but I can only find dates/money ect. Could anyone possibly point me in the right direction here. Many thanks.

public void setReference(String ref) throws IllegalLibraryReferenceException
       {
       reference = ref; 

       }



Is This A Good Question/Topic? 0
  • +

Replies To: Basic String Formatting

#2 tectonic.software  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 88
  • Joined: 12-February 10

Re: Basic String Formatting

Posted 15 March 2010 - 12:51 PM

Well, In this case I would use a Regular expression.

More info here: http://java.sun.com/...ssential/regex/
public void setReference(String ref) throws IllegalLibraryReferenceException
       { int number = findNumbers(ref);
            if(number > 99 && number < 1000 && findNonNumbers(ref).length() == 2){
                  reference = ref; 
            } else {
                  throw new IllegalLibraryReferenceException();
            }

       }


public static String findNonNumbers(String str) {
		return str.replaceAll("[0-9-]", "");
	}

public static int findNumbers(String str) {
		String result = str.replaceAll("[^0-9-]", "");
		if (result == "") {
			return -1;
		} else {
			return Integer.parseInt(result);
		}
	}


This post has been edited by tectonic.software: 15 March 2010 - 12:52 PM

Was This Post Helpful? 1
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Basic String Formatting

Posted 15 March 2010 - 12:53 PM

Alright, so this is definitely not the easiest way of going about it, but you could use regular expressions! Yay!

Here is what I came up with:
R[ABC][0-9]{3}

This will match the letter R, followed by either A, B, or C, and three digits

You could then use the matches method of the String to determine if it is formatted correctly:
if(!ref.matches("R[ABC][0-9]{3}")
         //throw error
      else
         //string is formatted correctly


edit: looks like out of the answers so far, this is the easiest :P

This post has been edited by erik.price: 15 March 2010 - 12:55 PM

Was This Post Helpful? 1
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Basic String Formatting

Posted 15 March 2010 - 12:54 PM

What you could do is to split the string into a character array, and then use the methods of the Character class to test for correctness. http://java.sun.com/.../Character.html

So, I would do something like this:
        public void setReference(String ref) throws IllegalLibraryReferenceException
       {
           char[] charStr = ref.toCharArray();
           
           // Make sure the first 2 characters are letters and uppercase.
           for (int i = 0; i < 2 i++)
               if (!(Character.isUpperCase(charStr[i]) 
                       && Character.isLetter(charStr[i])))
                   throw new IllegalLibraryReferenceException(message);
           
           // Make sure the next three are numbers
           for (int i = 2; i < charStr.length; i++)
               if (!Character.isDigit(charStr[i]))
                   throw new IllegalLibraryReferenceException(message);

           // If it gets here, it's ok.
           reference = ref; 

       }


Was This Post Helpful? 1
  • +
  • -

#5 davidsouth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-March 10

Re: Basic String Formatting

Posted 15 March 2010 - 01:28 PM

Thanks for all the help everyone. I've got this to work now. I've looked into all these methods just to progress my general Java knowledge. Very much appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Basic String Formatting

Posted 15 March 2010 - 01:32 PM

Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1