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

Thread: Trying to create an alphabet-only string of any length

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Trying to create an alphabet-only string of any length

    package test2problem3;
    import java.util.Random;
    public class Test2problem3 {
     
        public static void main(String[] args) {
     
     
     Random gen= new Random();
     
     String ranString="";
     int len=gen.nextInt(20)+1;
     int ranCode=0;
     
     int count = 0;
     
     while (count <len)
     {
         ranCode=gen.nextInt(58)+65;
         {
         if (((ranCode>=58)&&(ranCode<=64))||((ranCode>=91)&&(ranCode<=96)))
             System.out.print (ranString);
         else     
             ranString += (char)ranCode;
             count++;
     
             System.out.print(ranString);
         }    
     }
     
     
        }
    }

    I'm a beginner java student. this is what I got so far, with a knowledge I have.

    My Q's:
    1. There seems to be a slight pattern on some, is there any way to remove that?
    2. Is there any wrong/unnecessary parts to my code?
    3. Can I know a more efficient way of solving this type of problem, for a future reference?

    Thank you.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trying to create an alphabet-only string of any length

    The indentation is poor.
    There should be some type of comment describing the purpose/use of the code.
    There is a set of brackets which can be deleted.

    ccCcCycCyMcCyMTcCyMTgcCyMTgecCyMTgeH
    wwRwRLwRLAwRLACwRLACwRLACwRLACVwRLACVbwRLACVbawRLA CVbarwRLACVbarSwRLACVbarSOwRLACVbarSOs

    YYFYFSYFSCYFSCfYFSCfFYFSCfFeYFSCfFeuYFSCfFeumYFSCf FeumhYFSCfFeumhB
    -----compare above to below------
    Y YF YFS YFSC YFSCf YFSCfF YFSCfFe YFSCfFeu YFSCfFeum YFSCfFeumh YFSCfFeumhB
    See why there is a pattern and why more than 20 characters are printed in each run.

Similar Threads

  1. Replies: 2
    Last Post: September 29th, 2012, 07:42 PM
  2. Weird String .length() method
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 11th, 2012, 10:30 AM
  3. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM
  4. [SOLVED] Problem using the length of a string to bound the number of iterations of a for loop
    By dtitt3 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 3rd, 2011, 01:44 PM