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: stuck on an assignment and need help with repetition

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Location
    Ballarat, Australia
    Posts
    3
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb stuck on an assignment and need help with repetition

    Hi, Im new to java and have been issued my first assignment, ive coded my menu and am beginning to code my first sub section of the menu which is a character patter printer, basically what it does is prompts the user to enter the ammound of rows and the character they would like to use.

    please enter number of rows : 5
    please enter a character : c

    output :

    c
    cc
    ccc
    cccc
    ccccc

    i cant figure out how to get the character to appear on the line more then once. here is my code for when the user selects a, bare in mind that i am new and this is my first assignment and loops may nit be the correct way to go about it, but its all i could think of with what i've learned so far

    // Menu selection A, character pattern, will ask user
    // for number of rows and a character to enter

    if (menuChoice == 'A' || menuChoice == 'a'){

    // If a number exceeding 25 is entered, the loop wont allow
    // the program to continue untill a number between 1 - 25
    // is entered
    while (patternRows > 25) {



    System.out.println("Welcome to Character pattern printing");
    System.out.println("Please enter # of Rows (max 25)");
    System.out.print("Wont allow a number higher than 25 : ");

    // Taking user response and storing it in the
    // variable patternRows
    patternRows = scanner.nextInt();
    }


    System.out.print("Please enter a character : ");
    patternChar = charInput.readLine().charAt(0);

    while (linesPrinted != patternRows){
    System.out.println(patternChar);
    linesPrinted ++;
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: stuck on an assignment and need help with repetition

    If you're like adding only one more character for each row, and not having variations, for instance

    you're

    a
    aa
    aaa
    aaaa
    aaaaa

    and not

    a
    aa
    a
    aaaa
    aa

    Then you should use a String to keep adding one char and perhaps use a for or while loop.

    You could have, before the for or while loop, a String variable called chars

    String chars = "";

    Also, I'd recommend that you have only 1 Scanner object unless you're reading in from a file as well as from the keyboard.

    After you've read in the char value and the number of rows, you could

    for (int i =0; i < patternRows; i++)
    {
    chars = chars + String.valueOf(patternChar);
    System.out.println(chars);
    }

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    teeej86 (March 21st, 2011)

Similar Threads

  1. [SOLVED] Using a For loop for repetition
    By cb5950 in forum Loops & Control Statements
    Replies: 7
    Last Post: March 8th, 2011, 05:53 PM
  2. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  3. java Logic Random with out repetition
    By Jhovarie in forum Loops & Control Statements
    Replies: 1
    Last Post: January 13th, 2011, 03:25 PM
  4. Totally stuck on this assignment
    By Sgiahatch in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2010, 04:25 PM
  5. [SOLVED] changing character repetition
    By vendetta in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 16th, 2010, 06:16 PM