Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: String verification.

  1. #1
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default String verification.

    For an assignment I'm to make a program that takes in a user input as a password containing at least ten characters and at least one letter and one number.

    I've got the ten character verification down I think, but I didn't see anything in the book yet (or if I did it didn't go into much detail on it), that explains much about verifying a string has a set number of characters/numbers.

    The lesson did cover the StringBuilder, and looking over the lesson it did mention something like "public StringBuilder (String strArg)", although I'm a little confused on the description of it. It says " Arguments: A String object (that will be copied to the StringBuilder)
    Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string."

    To me this sounds like you could set say :
    public StringBuilder (String qwerty1);
    and as long as the user input was at least five characters and a number it would pass... Or at least that's what it sounds like to me, I could be dead wrong on it.

    If someone could clarify this a bit more or point in me in the correct direction in terms of googling criteria I'd be much obliged.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 832 Times in 772 Posts
    Blog Entries

    Default Re: String verification.

    For an assignment I'm to make a program that takes in a user input as a password containing at least ten characters and at least one letter and one number.
    Take a look at regular expressions (regex) - Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes). That link might be overwhelming at first, but regular expressions are an important concept for String manipulation and will readily allow you to accomplish String validations. You could do this using basic String manipulation, but a regex is much easier

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: String verification.

    First, let's clear up a few things with the small sample you provided. StringBuilder is a class. You can declare a variable to be of type StringBuilder. That is to say the variable can hold/refer to StringBuilder objects. The variable must have a valid identifier (a.k.a. name).

    StringBuilder my_string_builder; // declares a variable named my_string_builder which can hold StringBuilder objects

    You can create StringBuilder objects using the StringBuilder's constructor. A constructor is a special method inside of StringBuilder with the same name as the class, StringBuilder. To call a constructor you must use the new keyword. Some classes can have more than one constructor defined. As you indicated, there's a constructor which takes a String object and creates a StringBuilder object initialized with the String's data.

    new StringBuilder("a test string");

    Let's put the two parts together and create a StringBuilder object and assign it to a variable so we can use that variable to manipulate that StringBuilder object.

    StringBuilder my_string_builder = new StringBuilder("a test string");

    You don't need to provide a String literal, you just need to give it a valid String object.

    String test_string = "this is a string";
    StringBuilder my_string_builder = new StringBuilder(test_string);

    Now that bit out of the way, why do you think you need a StringBuilder object? What does the StringBuilder object give you? I would suggest reading the StringBuilder JavaDoc to find out what a StringBuilder is.

    The very first sentence describes exactly a StringBuilder is.

    A mutable sequence of characters.
    mutable means you can change it. Strings in Java, on the other hand, are immutable. That means once you have a String object it will be the same from the time it's created to the time it's destroyed. Note that I was careful to say that a String object is immutable. String variables can be changed to refer to other Strings if they're not declared with the final keyword.

    final String str1 = "a fine day";
    String str2 = "Java programming";
    str1 = str2; // compile error, the variable str1 can't be made to refer to a different String object.
    str2 = str1; // perfectly fine, str2 can refer to the same String object str1 refers to.

    So back to the original question of why do you need a StringBuilder object, and why isn't a String object sufficient?

    To answer this question think about what steps you have to take in order to figure out if the password inputted is valid. How would you (using a piece of paper and a pencil) figure out if the password contains at least one letter and one number.

Similar Threads

  1. Project Idea / verification
    By zenaphor in forum Java Theory & Questions
    Replies: 4
    Last Post: July 3rd, 2012, 07:11 AM
  2. Scripting Rules for User verification
    By jwarren in forum Java Theory & Questions
    Replies: 0
    Last Post: June 29th, 2012, 08:26 AM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. Java - Crypto ISBN number verification, GUI program
    By djl1990 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2011, 11:22 AM
  5. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM