• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pyramid of numbers

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this?

[program deleted]
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Carey Junilu means that the space in line 74 should be changed to a dot so that you can see how the actual spacing is working.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Donna Perl.

Thanks you for your contribution, but as you can see, I've deleted the code you provided.  This is because it is a complete solution and in the Beginning Java forum we don't provide complete solutions.  There are several reasons for this.  One is that we want the OP to learn and come to their own conclusions.  Another is that if the problem is a homework assignment, we don't want to do the OP's homework for them.

It's okay to provide code snippets or a working program that is not a solution but provides an illustration of a point.  And if the OP has already provided a complete, working solution, sometimes we will show how the solution can be optimized.  
 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think Carey Junilu means that the space in line 74 should be changed to a dot so that you can see how the actual spacing is working.



i got the solution already without using multiple methods. i tried using dot instead of space which is not working. I think you can move this question from beginning java to later stage and post the code solution at this point as i was able to get the solution myself but struck at later stage
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is still a beginner level problem. Post your solution code if you want to compare it to how others have solved it.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


above is my solution.

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is the correct pyramid?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
either is fine for me
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my solution gives below decending output

123
integerArray is [0, 0, 0]
Splitted the String up into an array that has 1 element per character in the String [1, 2, 3]
integerArray before sorting[1, 2, 3]
integerArray after sorting[1, 2, 3]
  3
 2 2
1 1 1



not sure on how to get ascending output
Also not sure on how to seperate printing space and priting digits to two different methods. I replaced space with . and printing at wrong location as below. I was thinking it is not possible to separate my code two separate methods as both space and digits depend on common for loop of noOfRows




312
integerArray is [0, 0, 0]
integerArray before sorting...[3, 1, 2]
noOfRows value in printSpaces--->3
......noOfRows value is printElements--->3
3
2 2
1 1 1
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


which collection object should i use for ascending order number result in rows?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on your latest posts, it isn't really clear have you solved this exercise or not, I see some potential outputs you posted, but not sure if these aren't dummy.
However, couple of days ago I read you have solved this exercise, but it seems I misunderstood you.

sai rama krishna wrote:which collection object should i use for ascending order number result in rows?

None. You have sorted numbers within an array already before you printing them out. Your problem is, that probably you aren't printing them in the right order, or simply - printing from the other end. Don't know now as haven't seen your code. Anyway, posting your code won't help much. You need either start over, by expecting exercise will start making sense, or put this exercise away and come back to it after some time, maybe it is too complicated for you at the moment.

But if you still have passion to try to solve this - try to read this thread once again, and in addition of that - delete the code you have now, and start over - I believe that would be beneficial and might you'd find the solution.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My, this thread has gone on for a long time. Apart from Liutauras' suggestion that you go back to the beginning of the thread and re‑read it, I shall make a different suggestion. Well, several. The first is to write a loop that printsYou may prefer to print the numbers on separate lines.
The second is to generalise that to print 1…n where n is any positive int. An n with more than eight digits is in probably best avoided unless you have a very long time available to watch the screen.

You shou‍ld achieve both within about five minutes.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



numbers...
0 1 2 3 4 5 6 7 8 9
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, we will be printed 1 once at the first row, we will be printed 2 twice in the second row and so on. I am sharing java code for create pyramind.

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claudia Joseph, please read this:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

which appears on the contents page of this forum. It doesn't help people to provide them with a complete solution; in fact it may deprive them of the opportunity to learn for themselves. Don't be annoyed, but I have pulled rank and removed your solution.
It had some poor features, too, including too much use of <= and everything in the main method. The <= operator is not actually wrong, but < is easier to understand. The basic form of a for loop shou‍ld be
for (int i = 0; i < something; i++) ...
so start with that format. You will often have to change it, but that is what you start from. It shou‍ld be easy enough to print 1 2 3 4 5 6 7 8 9 in a loop headed
for (int i = 0; i < 9; i++) ...
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:


numbers...
0 1 2 3 4 5 6 7 8 9

Your indentation is incorrect partially because you are using tabs and partially because it is inconsistent. Look at lines 3 and 5.That is an incorrect form for an Java® identitier; it shou‍ld not contain underscores. Use forLoop instead. But when you are having difficulty writing loops in the first place, why make life more difficult for yourself by giving your class a package name?Nonononononononono. Never use i = i++ which is a notorious pitfall causing i to revert to its original value. That is why you used i++ twice.
Declare i inside the loop. See what I wrote in a recent post about the basic form for a for loop.

So far, so good. We have a bit of progress. You have got as far as 0 1 2 3 4 5 6 7 8 9. Do you need the 0? Let's get that to read 1 2 3 4 5 6 7 8 9. Then try 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1. Hints:-
  • 1: That runs from 1 to 9 and back. That is 17 separate numbers.
  • 2: Consider subtraction and use of Math#min or Math#max. You will have to write some numbers on a sheet of paper to work out which formula you are using.
  • 3: You do not require nested loops at this point. You can do all that in one loop.
  • You will probably require nested loops later.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    corrected to below
    numbers...
    1 2 3 4 5 6 7 8 9

    looking at decrementing case
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    numbers...
    1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

    Not sure yet in one loop?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Now do it all in one loop. You will need Math#max or Math#min, but maybe not both. Remember what I said about 9 and 17.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:. . .. . .

    You would appear not to have read what I said about for loops. Declare the loop variable inside the loop not before it. You are writing Java® not C. Start from 0; there is no need to change to 1. And don't use 10 if you want to go up to 9:-To get this printed

    1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

    you will have to work out how to calculate the right number from the left number. The left number is i in that particular run through the loop and the right number is your printout. Remember n is 9 and you are printing a total of 17 numbers, as you can tell from the line numbers.That will print a diamond eventually. If you are only printing the top half, you might only need the easy part of the loop. But you will need to print other numbers. More about that to follow.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Once you have got that working, see if you can't print this.You may only need the top half of that sequence of numbers, down to line 9; the middle column tells you how many spaces you need.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    moved the loop variable into the loop.

    Thinking on how to do in one loop using Math.max or Math.min...
    not ideas floating in my mind yet
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    MAth.max gives max of given two or n number of integers right.

    https://www.tutorialspoint.com/java/lang/math_max_int.htm

    How we use that formula here to print 1 till 9 then backwards?
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    something like above it gave below output?
    1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    can some one please post the deleted code to understand the solution in the entire perspective as i made lot of effort to reach where i am with this. Also thousand comments cannot be equal to one line of code?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I didn't say to use Math#max, but max or min. You need to write down the numbers on paper or something like that, along with the lop index. A bit of addition and subtraction and use of max or min (maybe not both) will give you that pattern. It is quite easy if you put a few minutes' thought into it.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I forgot that if you only want a triangle like this:-you might only need the first half of the sequences, so max and min may both be unnecessary.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    so max and min may both be unnecessary.

    4

    not clear. can you please elaborate?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you only displayyou can do the arithmetic with the loop index and the length of the loop without needing max or min. All you have to do is work out the relationship between how many spaces and how many numbers you are printing.
    If you want this sort of thing, you can use max or min but maybe not both:-The use of max or min will allow you to draw that ⋄ in one loop, with inner method calls:-The printSpaces and printNumbers methods will have loops in, so you can nest the loops indirectly. You can of course use the printSpaces and printNumbers methods to print
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator



    528
    integerArray is [0, 0, 0]
    integerArray before sorting...[5, 2, 8]
    noOfRows value in printSpaces--->3
    ......

    i got above output when i followed similar steps?
    please advise
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think we shall have to let you out of your misery:-Note in the first line i = 0, so if n = 4, n − i − 1 = 3, so you print 3 spaces, then i + 1 = 1, so you print 1 once. In the next line where i = 1, you get two spaces and print 2 twice, etc. The %-2d bit (line 19) means the number occupies two spaces, aligned to the left. You can move line 21 to after line 4 if you prefer. I don't have a Java® program to print a diamond, only one in C, but I shall try translating that into Java®:-Run it like this
    java Diamond 11
    It doesn't work well with even numbers; you can only make a nice symmetrical diamond with odd numbers.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    above code gave below output





    but my requirement is ascending rder equilateral triangle like below when i pass like 4213

     
    Carey Brown
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you will try that also
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i am giving input from console so i need to use scanner and change to integer array and then string aray then sort etc
     
    Carey Brown
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:i am giving input from console so i need to use scanner and change to integer array and then string aray then sort etc


    What's your point? Do you have a specific question?
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:i am giving input from console so i need to use scanner and change to integer array and then string aray then sort etc



    You really don't. If you scan in a string and split it, the characters will sort the same way that numbers sort. There's really no point in jumping through artificial hoops of parsing the chars to int.
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @OP: I really think you can benefit from reading V. Anton Spraul's book, Think Like a Programmer. In that book, he has one early example that lays out a strategy for solving this type of problem. It starts out by printing out a square of stars/asterisks. The next step would be to print an increasing number of stars. Then the next step would be to the appropriate number of spaces before you print the stars.

    The strategy is to not try to solve the difficult problem but to find something simpler but very similar. When you see how a simpler problem is solved, then it's an easier jump to a more difficult problem, rather than try to solve the difficult problem directly.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Definitely I will order that book soon.

    When I enter number say 4132 in console and get using code something like
    Int[] num=new int[]
    Scanner SC=new Scanner(System.in)
    For(int I =0;I<Len;I++)
    num[i]=SC.nextInt();

    I get values as char or int or string orsomething else?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you enter that, you will get one int 4132. If the code compiles at all, that is.
    You are making an easy task really difficult, and one way to make it difficult is to try several different things together. You have the task of printing a diamond, triangle, or whatever. You also seem to want to read something from the keyboard. Right. Those are two separate tasks which you ought to handle separately. If you want to divide the number into separate characters and sort them, that is something different again. Again it should be done separately.
    And what do you want to happen if I pass a number like 4023 or 61423?
     
    reply
      Bookmark Topic Watch Topic
    • New Topic