Results 1 to 8 of 8

Thread: Loop Help

  1. #1
    HeavyD is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default Loop Help

    Hi,
    I need help. Im new to java and am trying to figure something out.

    I want to create a pattern loop where the user enters the number of rows to output and also what to output.

    e.g

    How many rows: 5
    Character to be printed: X

    X
    XX
    XXX
    XXXX
    XXXXX

    Would anyone have any clue where i start or show me the coding to help me understand better??:(

    Thanks

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Lets say you keep number of rows in numRows variable and character to be printed in charToPrint variable. Then the code will be like this:

    Java Code:
    for (int i=1; i<=numRows; i++) {
    
       for (int j=1; j<=i; j++) System.out.print(charToPrint);
       System.out.println();
    
    }
    I guess you already know how to get a character and integer from user. If not try google. If something is unclear let me know.

  3. #3
    HeavyD is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    I have the genereal idea but not sure how to do it. Any other help would be greatly appreciated. I'll keep at it.
    Thanks

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    This code shows how you can get a String from user:

    Java Tips - Reading Text from Standard Input

    After you get String you can convert it to integer or character. For converting it to character, you can call charAt(0) method of that string. And you can see the following tip to convert it to integer value:

    Java Tips - Conversion from String to integer

    Now, you just need to join these pieces together.

  5. #5
    HeavyD is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    I've worked out how to let the user input a number and character but still having a problem. Here is the code below. Can anyone tell me what im doing wrong? This is the whole program i created, some which is not complete but its just the FIRST section, pattern section that i need help with.

    Java Code:
    import java.util.Scanner;
     
    public class assignment1 {
     
           public static void main (String[] args) {
                  Scanner keyboard = new Scanner(System.in);
                  System.out.println();
                  System.out.println("Print Pattern and Reverse String Menu");
                  System.out.println();
                  System.out.println("A. Print a pattern");
                  System.out.println("B. Reverse a string of text");
                  System.out.println("X. Exit the programme");
                  System.out.println();
                  System.out.print("Select an option from the menu (A, B or X): ");
                  char option = keyboard.next().charAt(0);
     
                  if (option == 'A' || option == 'a') {
                      System.out.println();
                      System.out.print("Enter the number of rows in the pattern to be printed: ");
                      int rows = keyboard.nextInt();
                      System.out.print("Enter character to be printed: ");
                      char character = keyboard.next().charAt(0);
     
                      for (int i = 1; i<=rows; i++) {
                          for (char j=1; j<=i; j++)
                              System.out.print(character + " ");
                          System.out.println();
                      }
                  }
    
                  else if (option == 'B' || option == 'b') {
                      System.out.println();
                      System.out.println("Enter a string of text to be reversed: ");
                      String text = keyboard.nextLine();
                      int last = text.length();
                      String reverse = text.substring(last,0);
                      System.out.println("The reverse of the string of text you entered is: " + reverse);
                      System.out.println();
                  }
                  else if (option == 'X' || option == 'x') {
                    System.out.println("Exiting the programme...");
                    System.out.println();
                    System.exit(0);
                  }
    
                  else {
                     System.out.println("Error! You did not select a valid option from the menu");
                  }
           }
    }
    Last edited by JavaBean; 07-09-2007 at 07:10 AM. Reason: Code placed inside [code] tag.

  6. #6
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    As far as i understand your problem, that loop should be:

    Java Code:
    for (int i = 1; i<=rows; i++) {
         for (char j=1; j<=i; j++)
             System.out.print(character);
         System.out.println();
    }
    This should work, what is wrong with this?

  7. #7
    relics is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    hi i have a similar problem i dont know if you could help but i needed to print a pattern that looks like a triangle

    x
    xx
    xxx
    xx
    x
    i cant quite figure it out any help would be much appreciated.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    Please start your own thread for a new question.
    Can you write a for loop that loops for the number of lines you want to print out?
    In the loop can you detect which line you are printing on?
    Can you make some logic to determine the number of Xs to output depending on which line you are printing on?

    If you can do the above, then you've got your solution.
    If you have problems with any of the above, please explain.

Similar Threads

  1. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 06:44 AM
  2. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  3. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 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
  •