Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 19-02-2010, 12:47 PM
Junior Member
 

Join Date: Nov 2009
Posts: 9
Thanks: 5
Thanked 1 Time in 1 Post
davie is on a distinguished road
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:

Java Code
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



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 25-02-2010, 04:38 PM
Junior Member
 

Join Date: Nov 2009
Posts: 9
Thanks: 5
Thanked 1 Time in 1 Post
davie is on a distinguished road
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:

Java Code
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; 25-02-2010 at 05:18 PM.
Reply With Quote
  #3 (permalink)  
Old 25-02-2010, 08:17 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.

Java Code
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";
}
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
davie (11-03-2010)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To: Add line numbers to your JTextArea Freaky Chris Java Code Snippets and Tutorials 5 01-04-2010 10:08 AM
help me .. about creating random numbers soldier Java Theory & Questions 2 21-12-2009 12:24 AM
Roman Numbers chronoz13 What's Wrong With My Code? 14 12-12-2009 06:19 AM
How to reverse a String using java.lang.StringBuilder JavaPF Java Code Snippets and Tutorials 0 22-07-2009 02:42 PM
Random numbers Pooja Deshpande Java SE APIs 8 05-06-2009 09:36 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jbutton java jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:51 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.