Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

problem with string

string

  • Please log in to reply
5 replies to this topic

#1 mr mike

mr mike

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 03 December 2010 - 01:45 PM

I am trying to create a boolean method using the string matches() method. I want to display the phone number to be true either when the number is 567-345-4567 or just 433-3456. I am also having trouble with the city method, cities with either one name ie. Springfield or more ie. West Haven or Ft. Campbell.

HERE IS THE CODE:

private static boolean isLegalCity(String city) {
        return city.matches("([A-Z]+[a-z]+|\\s[A-Z]+)*");
    }
 private static boolean isLegalPhoneNumber(String phone){
        return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");
    }

HERE IS THE COMPLETE CODE:
mport java.util.Scanner;

/**
 *
 * @author mike
 */
public class Lab20 {

    private static Scanner scan;
    private static String fName, lName, sAddress, city, state, zip, phone;

    /**
     * Check for legal first name
     * @param fName
     * @return first name legal or not
     */
    private static boolean isLegalFirstName(String fName) {
        return fName.matches("[A-Z][a-zA-z]*");
    }// end of isLegalFirstName

    /**
     * Check for legal last name
     * @param lName
     * @return last name is legal or not
     */
    private static boolean isLegalLastName(String lName) {
        return lName.matches("[a-zA-z]+([ '-][a-zA-Z]+)*");
    }// end of isLegalLastName

    /**
     * Check for legal street adress
     * @param sAdress
     * @return street is legal or not
     */
    private static boolean isLegalStreetAddy(String sAdress) {
        return sAdress.matches("\\d+\\s+([A-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
    }// end of isLegalStreetAddy

    /**
     * Check for legal city entry
     * @param city
     * @return legal city or not
     */
    private static boolean isLegalCity(String city) {
        return city.matches("([A-Z]+[a-z]+|\\s[A-Z]+)*");
    }// end of isLegalCity

    /**
     * Check for legal state
     * @param state
     * @return legal state or not
     */
    private static boolean isLegalState(String state) {
        return state.matches("([A-Z]+[A-Z])*");
    }// end of isLegalState

    /**
     * Check for legal zip
     * @param zip
     * @return legal zip or not
     */
    private static boolean isLegalZip(String zip) {
        return zip.matches("\\d{5}");
    }// end of isLegalZip

    /**
     * Check for legal phone number
     * @param phone
     * @return phone number is legal or not
     */
    private static boolean isLegalPhoneNumber(String phone){
        return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");
    }// end of isLegalPhoneNumber

    /**
     * Display either all valid or specify the invalid entries
     * @param fName first name
     * @param lName last name
     * @param street street address
     * @param city
     * @param state
     * @param zip
     * @param phone
     */
    private static void dispValidString(String fName, String lName, String street,
            String city, String state, String zip, String phone) {
        String invalid = "";
        if (!isLegalFirstName(fName)) {
            invalid += "Invalid First Name!\n";
        }// end of if
        if (!isLegalLastName(lName)) {
            invalid += "Invalid Last Name!\n";
        }// end of if
        if (!isLegalStreetAddy(street)) {
            invalid += "Invalid Street Address!\n";
        }// end of if
        if (!isLegalCity(city)) {
            invalid += "Invalid City!\n";
        }// end of if
        if (!isLegalState(state)) {
            invalid += "Invalid State!\n";
        }// end of if
        if (!isLegalZip(zip)) {
            invalid += "Invalid Zip Code!\n";
        }// end of if
        if (!isLegalPhoneNumber(phone)){
            invalid += "Invalid phone number!\n";
        }// end of if
        if (isLegalFirstName(fName) && isLegalLastName(lName)
                && isLegalStreetAddy(sAddress) && isLegalCity(city)
                && isLegalState(state) && isLegalZip(zip) && 
                isLegalPhoneNumber(phone)) {

            invalid += "Nothing was invalid.\nAll fields were entered "
                    + "correctly!";
        }// end of if

        // display the results
        System.out.println(invalid);
    }// end of dispString

    /**
     * Main method
     * @param args command line entry, not used
     */
    public static void main(String[] args) {

        // instanciate scanner for user input
        scan = new Scanner(System.in);

        // get user inputs and store values
        System.out.print("Enter your first name:  ");
        fName = scan.nextLine();
        System.out.print("Enter your last name:  ");
        lName = scan.nextLine();
        System.out.print("Enter your street address:  ");
        sAddress = scan.nextLine();
        System.out.print("Enter your city:  ");
        city = scan.nextLine();
        System.out.print("Enter your state:  ");
        state = scan.nextLine();
        System.out.print("Enter your zip code:  ");
        zip = scan.nextLine();
        System.out.println("Enter your phone number:  ");
        phone = scan.nextLine();

        // diplay message
        dispValidString(fName, lName, sAddress, city, state, zip, phone);
    }// end of main
}// end of Lab20 class

Thanks in advance
  • 0

#2 eafkuor

eafkuor

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 214 posts

Posted 04 December 2010 - 01:48 AM

Do you have to use the matches method?
  • 0

#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 04 December 2010 - 02:49 AM

I think for the number you want (with double backslashes in java)
([0-9]{3}\-){1,2}[0-9]{4}
  • ([0-9]{3}\-){1,2} a value between 0 and 9, 3 times followed by a dash.. 1 or 2 times.
  • [0-9]{4} a value between 0 and 9, 4 times.

About the city... When does it NOT match? As i see it now the city passes if it has 1 or more words.. but that's every String that's not empty imo.
  • 0

#4 twanbaten

twanbaten

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 05 December 2010 - 06:47 AM

private static boolean isLegalPhoneNumber(String phone){
        return phone.matches("([1-9]\\d{2}-)?\\d{3}-\\d{4}")); //
}
assuming that the phonenumber cannot begin with '0'.
  • 0

#5 mr mike

mr mike

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 05 December 2010 - 07:37 AM

The city only was only good when I put in a single String that started with a capitol letter, the second string would produce an invalid entry. Thanks for your help.
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 05 December 2010 - 07:39 AM

Yes, because you didn't include the space between 2 words in the regex
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download