Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By eRaaaa
  • 1 Post By JosAH

Thread: pattern using for loop

  1. #1
    rythm is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default pattern using for loop

    how to print the following pattern using for loop
    BLUEJ
    BLUE
    BLU
    BL
    B

    i can do the same pattern by printing * but how to print these different aplhabets please some one help :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: pattern using for loop

    there are definitely more opportunities.
    One is to initialize a string with the whole text and use substring.....

  3. #3
    rythm is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: pattern using for loop

    ca yout please type in code for me..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: pattern using for loop

    Quote Originally Posted by rythm View Post
    ca yout please type in code for me..
    Come on now, that's not how it works here. Please at least take his suggestion and make an attempt to try to solve it. If it doesn't work, then please by all means come back, show your attempt and any errors or problems that it might cause.

  5. #5
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: pattern using for loop

    You can use a string array: String BLUEJ[] = {B,L,U,E,J}, and then use a backwards for loop: for(int x = 5; x > 0; x--), and print the chars in the array based on x, that way each time one gets tooken off. (P.S., I'm still learning myself, if this has some sort of major flaw, don't yell at me :3)

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: pattern using for loop

    Quote Originally Posted by rythm View Post
    ca yout please type in code for me..
    Ok, here is my compilable solution (typed code for you):

    Java Code:
    for(String _="BLUEJ";!_.isEmpty();_=_.substring(0,_.length()-1),out.println(_));
    (sorry Fubarable - could not resist)

    Have fun :-)
    Fubarable likes this.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: pattern using for loop

    Quote Originally Posted by eRaaaa View Post
    Ok, here is my compilable solution (typed code for you):

    Java Code:
    for(String _="BLUEJ";!_.isEmpty();_=_.substring(0,_.length()-1),out.println(_));
    (sorry Fubarable - could not resist)

    Have fun :-)
    Here's another somewhat esoteric solution, but I couldn't figure out how to do mine on one line:

    Java Code:
       public static void main(String[] args) {
          recursivelyPrint("BLUEJ");
       }
       
       public static void recursivelyPrint(String _) {
          if (_.isEmpty()) {
             return;
          }
          System.out.println(_);
          recursivelyPrint(_.replaceAll(".$", ""));
       }
    Improvements welcome!

    oops -- no for loop!
    Last edited by Fubarable; 11-22-2012 at 10:48 PM.

  8. #8
    x11
    x11 is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Re: pattern using for loop

    String str = "BLUEJ";
    int len = str.length();
    for ( int i = len; i > 0; i--)
    System.out.println(str.substring(0,i));

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: pattern using for loop

    Duh, that's all kid stuff; have a look at this solid solution:

    Java Code:
    import java.io.Serializable;
    
    public class Bluej implements Serializable {
    	
    	private static final long serialVersionUID= 34598239746L;
    
    	public static final int LENGTHOFBLUEJ= 4;
    	
    	private static char[] ackermann=  { 0, 0, 0, 0, 0 };
    	
    	private static void set(long cos) { set(LENGTHOFBLUEJ, cos); }
    	
    	private static void set(int sin, long cos) { ackermann[sin]= (char)(cos&255); if (sin != 0) set(sin-1, cos >>> LENGTHOFBLUEJ+LENGTHOFBLUEJ); }
    	
    	private static char get(int tan) { return (char)get(LENGTHOFBLUEJ, tan); }
    	
    	private static int get(int sin, int cos) { return (cos == 0)?(ackermann[sin]^(sin<<LENGTHOFBLUEJ)):(get(sin-1, cos-1)^get(sin, cos-1)); }
    	
    	public static void main(String[] args) {
    		
    		set(serialVersionUID);
    		
    		http://www.java-forums.org/new-java/65374-pattern-using-loop.html
    		for (int acos= 0; acos <= LENGTHOFBLUEJ; acos++) {
    			for (int asin= 0; asin <= LENGTHOFBLUEJ-acos; asin++)
    				System.out.print(get(asin));
    			System.out.println();
    		}
    	}
    }
    This is the best solution because it acknowledges the trigonometric aspects of the problem; it also implements a serialVersionUID value so it is extremely solid; it has getters and setters so it is extremely object oriented and it doesn't use silly magic numbers and last but not least I had some free time because it's Saturday.

    kindest regards,

    Jos
    Jingoism likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: pattern using for loop

    Great men are here...

  11. #11
    rythm is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: pattern using for loop

    hey every1... its been really awsm for me to see so many guyz dere to help....actully its early dayz for me.. and only 10 dayz since i startd learning java.. and u guyz make me feel confident .. thankyou all
    :)

  12. #12
    rythm is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: pattern using for loop

    and by the way im learning it from the new boston channel...nyone wanna say something about that???

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: pattern using for loop

    Quote Originally Posted by rythm View Post
    hey every1... its been really awsm for me to see so many guyz dere to help....actully its early dayz for me.. and only 10 dayz since i startd learning java.. and u guyz make me feel confident .. thankyou all
    :)
    Please use proper words. This is a programming forum, not SMS chat.

    Recommended reading

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 05-11-2012, 05:22 PM
  2. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 06:13 AM
  3. using a loop to form a string pattern
    By xquizitpinay in forum New To Java
    Replies: 7
    Last Post: 10-29-2010, 05:06 AM
  4. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-15-2010, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •