Results 1 to 8 of 8
  1. #1
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default using a loop to form a string pattern

    So, I'm writing this code where I need to display a given pattern once a string has been inputted...

    Ex:
    Java Code:
    0      5     10
    |----+----|---
    STRING STRING
    the pattern is the length of the string and it's supposed to create that exact look, with the numbers on top and the pattern underneath it, then the string...

    so far my code is all screwed up:
    Java Code:
    import java.util.*;
    
    public class Pattern {
    
        public static void main(String [] args) {
    	 
    	 	 	Scanner input = new Scanner(System.in);
    			System.out.print("Enter String: ");
    			String s = input.nextLine();
    			 
            for(int i=0;i<s.length();i++) { //This works out the number of lines       
    				String line = "";
                    for(int a=0;a<4;a++) {
                        line+="-";
                    }
    
                    for(int z=0;z<(i-5);z++) {
                        line+="+";
                    }
    
                    System.out.println("|" + line);       
            }
    		  System.out.println(s);
    
        }
    
    }

    the output is this:
    Enter String: STRING STRING
    |----
    |----
    |----
    |----
    |----
    |----
    |----+
    |----++
    |----+++
    |----++++
    |----+++++
    |----++++++
    |----+++++++
    STRING STRING

    I havent even started on the second part of that pattern code....
    Last edited by Fubarable; 10-29-2010 at 03:53 AM. Reason: Moderator Edit: Code tags added

  2. #2
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    I've worked the code to look like this:

    import java.util.*;

    public class Pattern {

    public static void main(String [] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter String: ");
    String s = input.nextLine();

    for(int i=0;i<s.length();i++) { //This works out the number of lines
    String line = "";
    for(int a=0;a<4;a++) {
    line+="-";
    }
    System.out.print("|" + line + "+" + line);
    }
    System.out.println(s);

    }

    }

    with the output:
    Enter String: STRING STRING
    |----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----STRING STRING


    something is still wrong with it...

  3. #3
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    I've gotten my code this far, to produce this output, i'm so close to it doing exactly what I want it to do...

    OUTPUT:
    0 5 10 15 20 25 30 35 40 45 50
    |----+----|----+----
    this is a long string, lets see how much i can write

    and this is my code:
    import java.util.*;

    public class Pattern {

    public static void main(String [] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Enter String: ");
    String s = input.nextLine();

    for(int number=0; number <= s.length(); number +=5){
    System.out.print(number + " ");
    }
    System.out.println("");
    for(int i=0;i <= s.length();i += s.length()) {
    String line = "";
    for(int a=0;a<4;a++) {
    line+="-";
    }
    System.out.print("|" + line + "+" + line);
    }
    System.out.println("\n"+s);

    }

    }

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default My effort

    Hi xquizitpinay,

    I liked you problem so much that I could not resist to try to solve it. Here's my code.

    [moderator edit: full code solution removed]

    It gives this result:

    Java Code:
    0    5    10   15   20   25
    |----+----|----+----|----+----
    This is quite a long string...
    The magic about how much spaces to add to the ruler is not so very neat but it works...

    Cheers,
    Erik
    Last edited by Fubarable; 10-29-2010 at 12:59 AM. Reason: Moderator edit: full code solution removed

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by venerik View Post
    Hi xquizitpinay,

    I liked you problem so much that I could not resist to try to solve it. Here's my code.
    ....

    Rather than spoon feeding the OP a full code solution, wouldn't he be better off getting suggestions on how to improve his own code? Otherwise you kind of cheat him out of the benefit of thinking for himself and creating his own code.

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Ah, good point. I'll think of that the next time.

    I'm new here so bare with me please.

    Erik

  7. #7
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    With that in mind... could someone help me then with my code?

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

    Default

    You have nested for loops which is akin to multiplication. If you have an outer loop that loops 3 times and an inner loop that loops 7 times, then the whole thing will loop 21 times, and this is not what you want to do.

    Instead you want to create 2 Strings (the third one, printing s is trivial), and this will just two for loops in series, not nested. So go through the logic of the first loop -- walk through it and try to figure out when to print a number and when to print a space. Myself, I use the mod operation, the one that uses the % operator, to be able to tell when a number is divisible by 5. You will need to take extra care when the number is 10 or more since it will mean less spaces are needed.

    Then for the next loop, you'll print out the |----+----|----+---- part. Again, use the mod operator to figure out when to print the appropriate character.

    Give it a try. You'll probably succeed in a few tries with some decent thought. And if you don't, please come back with your code.

Similar Threads

  1. String matching a pattern
    By vividcooper in forum New To Java
    Replies: 7
    Last Post: 01-07-2010, 10:30 PM
  2. How to validate a string in the form E25/8938/2009
    By nmvictor in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:37 PM
  3. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 12:32 PM
  4. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 PM

Posting Permissions

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