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.
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
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]
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
integerArray is [0, 0, 0]
integerArray before sorting...[3, 1, 2]
noOfRows value in printSpaces--->3
......noOfRows value is printElements--->3
1 1 1
However, couple of days ago I read you have solved this exercise, but it seems I misunderstood you.
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.
sai rama krishna wrote:which collection object should i use for ascending order number result in rows?
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.
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 should achieve both within about five minutes.
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.
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.
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 should 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 should 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++) ...
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 should 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.
sai rama krishna wrote:
0 1 2 3 4 5 6 7 8 9
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:-
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
sai rama krishna wrote:. . .. . .
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.
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
How we use that formula here to print 1 till 9 then backwards?
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
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 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.
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.
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?
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
The WEB SERVICES and JAX-RS Coursehttps://coderanch.com/t/690789/WEB-SERVICES-JAX-RS