Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Printing numbers as asterisks & spaces

    Hello, forum. I have a problem where I need to print a users number input and print that number as a series of asterisks and spaces as a result. It has to be in diagonal order so if the user types the number 2 the screen would show
    Java Code:
    *
      *
    The problem i am having is if the user types any number higher than 2, say 4 for example, the remaining numbers just print vertically instead of diagonally like this: User types 4 and the result is:
    Java Code:
    *  
                                                                                                                *
                                                                                                                *
                                                                                                                *
    I am trying to make the ouput go as follows when some types a number like 4
    Java Code:
     
    *
     *
      *
       *
    for the number 4 or any number the user types like 6 for example would be:

    Java Code:
    *
     *
      *
       *
        *
         *
    My code so far for this is here:



    import java.util.Scanner;
    public class Hwk2
    {//Start Class

    public static void main(String[] args)
    {

    Scanner input = new Scanner(System.in);

    String ast = "*";
    String space = "";
    int theNumber;




    System.out.println("Type a number: ");
    theNumber = input.nextInt();

    for (int count = 1; count<=theNumber; count++)
    {

    System.out.printf("*\n %s", space);


    }



    }//end main


    }//End Class


    Can anyone give me some insight on how o do this?
    Last edited by Cnos; 10-24-2011 at 05:37 AM.

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    String space = "";
    The string is empty. You didn't actually put a space in there.

    Also, for future reference, use [code] tags when posting source code.

  3. #3
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    I added the space = " "; but im having the same problem

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    Take a look at your first post. Notice how all the formatting of the stars has disappeared. We have no idea what the desired output should look like. Try wrapping code tags around how the stars should look.

  5. #5
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Sorry about the formatting issues originally with the first post but i have edited it and it now shows what im trying to do. Can anyone help?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    This is a simple task achieved with nested loops. The outer loop prints N (user input) rows. While the inner loop prints M spaces. Then after the inner (spaces) loop you print a single star and a new line.

  7. #7
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    So with the code i have now, according to what you said, I need to create the inner loop, print the statement outside of the loop? I have tried so several different combos of doing this with the same results

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    Java Code:
    System.out.printf("*\n %s", space);
    What your current code (above) does is print a star, then a newline and then a space (or whatever that String holds). It is not in the order it should be.

  9. #9
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Quote Originally Posted by Junky View Post
    Java Code:
    System.out.printf("*\n %s", space);
    What your current code (above) does is print a star, then a newline and then a space (or whatever that String holds). It is not in the order it should be.
    What order should it be? I have tried rearranging them with no luck

  10. #10
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Java Code:
     for (int count = 1; count<=theNumber; count++)
           
              {
              for(int now = 1; now<count; now++)
              {
              System.out.println(" ");
              }
    This is my outter and inner loop so far. What am i doing wrong in the inner loop, as i know the outter loop works?

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    Quote Originally Posted by Cnos View Post
    What order should it be?
    '
    I told you in reply 6.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    Do you know how many spaces should be printed on each line? Write the pattern out on graph paper if it helps. You should see a pattern.

  13. #13
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Quote Originally Posted by Junky View Post
    This is a simple task achieved with nested loops. The outer loop prints N (user input) rows. While the inner loop prints M spaces. Then after the inner (spaces) loop you print a single star and a new line.
    It maybe simple for you but i am a newbie and I have been trying this for several hours spanning a few days now. I was looking for a code example or help with my code

  14. #14
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    try this-

    for(int i=1;i<=theNumber;i++)
    {

    for(int j=1;j<i;j++)
    {
    System.out.print(" ");
    }
    System.out.println("*")
    }

  15. #15
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Cosmos

    Thank you very much! That works. I cant believe it was that simple. I was confused with the nesting part at first, but after studying your code, I now completely understand what was done which will help me out for future reference. Thank you again

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    @Cosmos

    Please do not post fully coded solutions. It is the task of the OP to derive a solution not yours. Although Cnos claims they have learnt from your code, 99% of the time people do not. They simply copy and paste your solution and hand it in. The next time they have a problem they will be back here expecting you to spoonfeed them again. We try to encourage people to think for themselves.

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Priting numbers as asterisks & spaces

    Java Code:
                                    // This is a simple task achieved with nested loops.
    for(int i=1;i<=theNumber;i++)   // The outer loop prints N (user input) rows.
    {
    
        for(int j=1;j<i;j++)        // While the inner loop prints M 
        {                  
            System.out.print(" ");  // spaces.
        }
        System.out.println("*")     // Then after the inner (spaces) loop you print a 
    }                               // single star and a new line.
    I agree with Junky. A better approach might have been to ask: "do you know what nested loops are?" Or advise: "break Junky's advice down into sections - first write a loop which prints N (user input) rows."

    Just my opinion, of course. By "better" I mean more useful, because I doubt whether drawing star patterns on the console is a skill which will be much referenced in the future. While knowledge of what the term "nested loops" means, and translating an English description of one, step by step, into code are skills which - if practiced and exercised - will be useful over and over again.

  18. #18
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Quote Originally Posted by Junky View Post
    @Cosmos

    Please do not post fully coded solutions. It is the task of the OP to derive a solution not yours. Although Cnos claims they have learnt from your code, 99% of the time people do not. They simply copy and paste your solution and hand it in. The next time they have a problem they will be back here expecting you to spoonfeed them again. We try to encourage people to think for themselves.
    The reason I came here in the first place was because i needed help. And he didnt give me the entire code, he showed me how to include the spaces the way I wanted them. From that code I was able to bring up a notepad test it and compare to the actual code to find out how it worked, which broke down the original loop incremented as many times as the user input, and the second loop incremented off the first loop minus one. I was not able to understand that until i could break it down this way with your instructions @Junky, so if you wanted to be a jerk-off about it you didnt have to reply. 99% of the people just copy and pasted it and hand it in. For your information, i didnt just copy and paste and hand it in, becasue the assignment was due yesterday. Everything from me and cosmos posts are after the fact. Yes I studied that code after handing in an incomplete assignment. I was not simply looking to copy and paste it, I needed help as to how to do it. Now I have a better understanding of for loops which i am studying, i told you i was a newbie and i had tried this for quite awhile before i decided to come to this forum. And @ pbrockway2, I was not sure at the time if i should do it with a nested statement or not, because i almost had it without the nested statement and I was told that this could be done with just one statement.

  19. #19
    Cnos is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default Re: Priting numbers as asterisks & spaces

    Quote Originally Posted by Cnos View Post
    The reason I came here in the first place was because i needed help.
    What else is a forum for?

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Priting numbers as asterisks & spaces

    There is a difference between help and being given the answer. I was helping you by trying to get you to arrive at the solution yourself. Just giving you the code does not help you. If you think it does then write a program that does this:
    Java Code:
    input = 4
    ****
     ***
      **
       *
      **
     ***
    ****

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  2. Turning numbers into asterisks.
    By BugginVT in forum New To Java
    Replies: 7
    Last Post: 02-25-2009, 07:38 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. printing an "E" out of asterisks via strings
    By hokieman07 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 05:45 AM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Tags for this Thread

Posting Permissions

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