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 5 of 5

Thread: String Builder questions

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default String Builder questions

    When I say:

    StringBuilder strBuilder = new StringBuilder();
           for(char alph = 'a'; alph <= 'z'; alph++)
                {
                    strBuilder.append(alph);
                    System.out.println(strBuilder);
                }

    output:
    "a
    ab
    abc
    abcd..."

    What I want to do now is generate a Char[][] based on user input for the dimensions. Then I will randomly generate characters from the alphabet to fill the array. Should I use the StringBuilder class for this? Or is there a simple way to generate random letters with math.Random?

    That is I know how to generate a random letter between a-z:
    Random r = new Random();
          char c = (char)(r.nextInt(26) + 'a');

    However; i'm not sure how to store those values into a Char[][]. So I figured maybe I could do it with StringBuilder.
    Last edited by vysero; April 13th, 2014 at 05:50 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: String Builder questions

    generate random letters with math.Random
    char are numeric values in the range: 'a' to 'z' and 'A' to 'Z'
    To see the value use an (int) cast: System.out.println((int)'a');
    The Math.random() method can be used in an expression to generate int values in the range: 'a' to 'z' which can be cast to char.

    Or the chars could be put into an array and a random index used to select the elements of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String Builder questions

    The part that is confusing me is filling my array with the randomly generated letters.

       public Build()
        {
            System.out.println("How many rows?");
            Scanner in = new Scanner(System.in);
            r = in.nextInt();
            System.out.println("How many colums?");
            Scanner in1 = new Scanner(System.in);
            c = in.nextInt();
            char[][] array = new char[r][c];
     
            Random r = new Random();   
            for(int i = 0; i<char[][].length; i++)
               {
                   char y = (char)(r.nextInt(26) + 'a');  
                   char x = (char)(r.nextInt(26) + 'a');
     
                   //how do amend x and y to the char[][] until it is filled with random letters?
               }
        }

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: String Builder questions

    Well you will need to iterate through each row and column to achieve that.

    Something like this would work
    for (int i = 0; i < r; i++)
         for (int j = 0; j < c; j++)
              array[i][j] = (char)(r.nextInt(26) + 'a');

  5. The Following User Says Thank You to Parranoia For This Useful Post:

    vysero (April 14th, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: String Builder questions

    Ah ic okay thank you very much.

Similar Threads

  1. HELP WITH FOR LOOPS! trying to concatenate without using string builder
    By janeeatsdonuts in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 08:00 AM
  2. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 08:40 PM
  3. How to use Builder Setters
    By copeg in forum Java Programming Tutorials
    Replies: 3
    Last Post: January 19th, 2012, 05:40 PM
  4. [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