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

Thread: How to put all char in loop to string?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to put all char in loop to string?

    I can't figure out how to have all of the random characters generated to go into the String. Below I can only get the last character to covert over to a String.

    Thanks in advance!

    System.out.println("Original random character string:");
            String printingString = "a";
     
            for (int i = 0; i < 50; i++)//loop to obtain 50 random characters
            {
                char randomChar = (char) ((Math.random() * 255) +32);
                System.out.print(randomChar);
                printingString = Character.toString(randomChar);  
            } 
     
            return printingString;
        }


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

    Default Re: How to put all char in loop to string?

    Use concatenation. Start with a String with an initial value and concatenate each new character to it.
    The + is the concatenation operator: "A" + "B" gives "AB"
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to put all char in loop to string?

    I don't understand what you are saying.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: How to put all char in loop to string?

    Do a internet search for Concatenating Strings
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] While Loop Using Char
    By Silvanoshei in forum Loops & Control Statements
    Replies: 1
    Last Post: October 20th, 2013, 12:14 PM
  2. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  3. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  4. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 09:04 AM
  5. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 08:58 AM