Jump to content

problem with string

- - - - -

  • Please log in to reply
5 replies to this topic

#1
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
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

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Do you have to use the matches method?

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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.

#4
twanbaten

twanbaten

    Newbie

  • Members
  • Pip
  • 6 posts
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'.

#5
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
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.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Yes, because you didn't include the space between 2 words in the regex




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users