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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: How to create a StringBuilder object that cntains equal numbers of a string

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default How to create a StringBuilder object that cntains equal numbers of a string

    Hi

    im working on a program and in the constructor i need to initialize a StringBuilder object with equal numbers of a the letters Y & N the constructor has an int argument which will specify how many of each String the StringBuilder will be initialized with. The code I have so far is:

    public Questions(int aNumber)
       {
          super();
          this.anwers = new StringBuilder(5);
       }

    but im now stuck as I dont know how to make itinitialize with equal numbers of eaxh string "Y" & "N"

    hoe someone can help


  2. #2
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default How to initialize an array to contain equal numbers of a string

    Hello guys

    im working on a program and in the constructor i need to initialize an array with equal numbers of the Strings "Y" & "N". the constructor has an int argument which will specify how many of each String the array will be initialized with. The code I have so far is:

    String answers[];
     
    public Questions(int aNumber)
       {
          super();
          anwers = new String[6];
       }

    but im now stuck as I dont know how to make itinitialize with equal numbers of eaxh string "Y" & "N"

    is this even possible or am I going about this the wrong way?

    I hope someone can advise me in some way as im completely stuck!

    Any help is much appreciated.
    Last edited by davie; February 25th, 2010 at 01:18 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to create a StringBuilder object that cntains equal numbers of a string

    Please don't ask the same question multiple times.

    Just fill the first half of the array with "Y" and the second half of the array with "N". You'll also want to initialize your array with two times the size of aNumber, not a static 6.

    answers = new String[2*aNumber];
    for (int i = 0; i < aNumber; i++)
    {
         answers[i] = "Y";
    }
    for (int i = aNumber; i < answer.length; i++)
    {
         answer[i] = "N";
    }

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    davie (March 11th, 2010)

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 11:22 PM
  2. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  3. Roman Numbers
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 12th, 2009, 02:19 AM
  4. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 10:42 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 05:36 AM