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

Thread: How to output a pyramid of stars from a recursive method using JOptionPane

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Post How to output a pyramid of stars from a recursive method using JOptionPane

    Hi,

    here is my code so far. I was told we need to use two recursive methods. Any help will be very much appreciated. Thank you in advance.

     
     
    package recursionexercise5;
     
    import javax.swing.JOptionPane;
     
    public class RecursionExercise5 {
     
        public static String saveStars(int n) {
     
            String stars, output;
     
            output = "";
     
            if (n == 0) {
     
                return "";
     
            } else {
     
                stars = "*";
     
     
                stars = stars + saveStars(n - 1);
     
               stars = stars + line(stars, n-1);
     
     
                System.out.println(stars);
     
                return stars;
            }
     
        }
     
        public static String line(String s, int num) {
     
            String st, output;
     
            if (num == 0) {
     
                return "";
     
            } else {
     
                return line(s += "\n", num - 1);
     
     
            }
     
        }
     
        public static void main(String[] args) {
     
            String sNum;
            int num, tStars;
     
            String stars;
     
            sNum = JOptionPane.showInputDialog("Enter the height");
     
            num = Integer.parseInt(sNum);
     
     
            stars = saveStars(num);
     
     
            JOptionPane.showMessageDialog(null, stars);
     
        }
     
    }
    Last edited by Norm; November 10th, 2019 at 04:43 PM. Reason: Fixed code tags - added ending code tag

  2. The Following User Says Thank You to nana223 For This Useful Post:

    newbietech (July 3rd, 2020)

  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to output a pyramid of stars from a recursive method using JOptionPane

    Can you post the program's current output and add some comments saying what is wrong and show the desired output?


    Note: The code has too many blank lines that make it harder to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to output a pyramid of stars from a recursive method using JOptionPane

    hi thank you for replying

    the program's current output depends on the number that was passed. example if 4 was passed the output would be :

    *
    **
    ***
    ****

    right now my code is only outputting " **** "

    I need help in getting the full pyramid to output to the JOptionPane dialog box. I was told to use 2 recursive methods as a hint. my saveStars method is working
    as it should but I know the problem is with my line method. The purpose of my line method is to save the string that it moves to a new line which makes sure the full pyramid is outputed to the JoptionPane dialog box

    here is the code again :


    import javax.swing.JOptionPane;
     
    public class RecursionExercise5 {
     
        public static String saveStars(int n) {
     
            String stars, output;
     
            output = "";
     
            if (n == 0) {
     
                return "";
     
            } else {
     
                stars = "*";
     
                stars = stars + saveStars(n - 1);
     
               stars =  stars + line(stars, n-1);
     
                System.out.println(stars);
     
                return stars;
            }
     
        }
     
        public static String line(String s, int num) {
     
            if (num == 0) {
     
                return "";
     
            } else {
     
     
                return line(s += s + "\n", num - 1);
     
            }
     
        }
     
        public static void main(String[] args) {
     
            String sNum;
            int num, tStars;
     
            String stars;
     
            sNum = JOptionPane.showInputDialog("Enter the height");
     
            num = Integer.parseInt(sNum);
     
            stars = saveStars(num);
     
            JOptionPane.showMessageDialog(null, stars);
        }
     
    }
    Last edited by Norm; November 10th, 2019 at 05:39 PM. Reason: Removed spaces from first code tag

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to output a pyramid of stars from a recursive method using JOptionPane

    purpose of my line method ...
    That should be included as comments in the code so anyone reading the code will know what the method is supposed to do.

    Please add comments to the code to describe what each method is supposed to do.
    Reading the comments should enable anyone that reads them to understand what the code is trying to do.

    The code still has too many blank lines.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    nana223 (November 10th, 2019)

  7. #5
    Junior Member
    Join Date
    Nov 2019
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to output a pyramid of stars from a recursive method using JOptionPane

    Sorry for not adding comments. I have been struggling with the recursion unit so I tend to forget to add comments because I was too focused. The part I am having trouble is getting the previous line of stars to be saved instead of it only outputting the final line of the pyramid. As a hint my teacher said to use two recursive methods. The second method is the one I am having trouble with.

    if 4 was passed into the saveStars method, the output should be :

    *
    **
    ***
    ****
    but it is only outputing "****" which is the final line.


    here is the code again:

     
    package recursionexercise5;
     
    import javax.swing.JOptionPane;
     
    public class RecursionExercise5 {
     
    //keeps adding stars to the the String "stars" until it reaches 0
    // param n - the amount of stars
        public static String saveStars(int n) {
     
            String stars, 
     
            if (n == 0) {
                return "";
            } else {
                stars = "*";
                stars = stars + saveStars(n - 1);
                //this is supposed save the previous line of stars and then add a new line
               stars =  stars + line(stars, n-1);
                return stars;
            }
     
        }
     
    //save the previous line of stars and then add a new line
        public static String line(String s, int num) {
            String st, output;
            if (num == 0) {
                return "";
            } else {
                return line(s += s + "\n", num - 1);
            }
     
        }
     
        public static void main(String[] args) {
     
            String sNum;
            int num, tStars;
     
            String stars;
            sNum = JOptionPane.showInputDialog("Enter the height");
            num = Integer.parseInt(sNum);
            stars = saveStars(num);
            //outputs the final pyramid of stars
            JOptionPane.showMessageDialog(null, stars);
     
        }
     
    }
    Last edited by nana223; November 10th, 2019 at 09:55 PM.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to output a pyramid of stars from a recursive method using JOptionPane

    I'm not very good with recursion. Here is a link to a forum that has more academic helpers: http://www.coderanch.com/forums
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to output information entered in JOptionPane Dialogue Boxes?
    By StaffordEmpire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2014, 03:00 PM
  2. Replies: 4
    Last Post: December 9th, 2013, 10:40 AM
  3. JOptionPane output assistance
    By dtooth71 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 10th, 2012, 10:59 AM
  4. help with recursive method
    By mflb94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 06:30 PM
  5. Problem with recursive method. Can you help?
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 7th, 2011, 05:44 PM

Tags for this Thread