• Post Reply Bookmark Topic Watch Topic
  • New Topic

java program, with increasing preceding white space with each line  RSS feed

 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to write a small program that takes the length of an input an uses the number of character in the string to state how many times it writes the string,
each string should be on a new line with each line starting with an extra (white) space.
e.g. the string argument "of" should print


and egg would print;


This is what I have been working with, and have tried several orders of loops etc.



any and all help appreciated thank you guys !
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't declare variables before you need them. Don't declare the loop variables before the loop; declare them in the loop heading:-
for (int i = 0; i < something; i++) ...
Use the ++ operator rather than writing + 1.

Start by writing in words of one syllable what you are planning to do. Make it really simple, so even I can understand it. That will make it easy to convert to code. If you can't turn your explanation into code, write it down here and we'll tell you it isn't simple enough

Use code tags round your output; I shall go back and add them for you. Use text in the dropdown list next to the code button.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: Start by writing in words of one syllable what you are planning to do. Make it really simple, so even I can understand it. That will make it easy to convert to code


I would like to add a new new white space at the start at each line.

so I could write the code (you see how idiotic this is) i would like to know how to do it using loops but i really cant think of the logic to do, sorry
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stef Aucky wrote:. . . I would like to add a new new white space at the start at each line. . . .
Definitely monosyllabic
At the start of which line? Before you print the number? Can you fit it inside the same loop? I agree the solution with the multiple ifs is pretty dreadful.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Don't the spaces need to go before the String?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each go round the loop, how much space do you want to print at the start of the line? Write it down. These things will then turn out to be not as hard as you thought.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
Don't the spaces need to go before the String?


haha yes it should however when i first tried with the "a" loop first, the space printed infront of the first line,
so i swapped them to prevent this it obviously still failed

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note also that you can concatenate strings with the "+" operator and that "" is an empty string. So,
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us get back to the small words.
The first time you go round the loop, how much space do you need at the start, and what is i?
The next time you go round the loop, how much space do you need at the start, and what is i?
The third time you go round the loop, how much space do you need at the start, and what is i?
And the fourth time? Can you see that some things are the same each time?
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so round the first loop i need 0 space i is 0
second time i need 1 space i is 1
etc. yes this is true but im unsure how to use this

I'm very new to programming so I'm sure it is simple, but i cant quite get it.
and in response to an earlier statement i was told that the convention to use is a = a + 1 instead of a++ due to being able to remember the statement if we want to increase by any given amount (or subtract for that matter)
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stef Aucky wrote:and in response to an earlier statement i was told that the convention to use is a = a + 1 instead of a++ due to being able to remember the statement
++ i'd say is more conventional than ... + 1. You might misunderstood the person whoever told you that.

So, as you rightly noticed from Campbell's advice, each time the 'i' value represents how many 'space' characters you want to print. How would you do that? Loop? Try loop approach, see if that works for you, maybe you'll find another ways to do later.

 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i tried this.

but i got this output

what is throwing me is looping multiple new lines each starting with a new amount of white spaces if it was a case of one line i would just loop the white space (as i have done) but i can only get this to ever put the white spaces on one of the lines no matter how i nest the loops or what order i put them in :'( stessed much lol
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the hardest part is, is to resist yourself from typing code lines by expecting to get the wannabe output. Stop that for now. Get pencil and piece of paper and workout the steps you need to do on paper, and only then try to code.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the formatting of your code does not make it easy to follow. This is how it should be formatted:

A problem is immediately apparent with your for loop variable, a, being used and initialized in multiple nested loops
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stef, also better if you'd use for the loop control the variables as i (for outer loop), j (nested), k (nested deeper). That way helps others to read you code easier as these variable names are used comonly among many developers.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem can be solved with just one for-loop and at most two for-loops, one nested in the other.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer the two‑loop solution at the moment; we can look at the one‑loop solution later. The exercise is probably intended to show the use of two loops.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would prefer the two‑loop solution at the moment; we can look at the one‑loop solution later. The exercise is probably intended to show the use of two loops.

this exercise is still unsolved, is there any chance you could tell me the logic, hope fully i don't need the code.
I'm beginning to feel rather stupid, as i know it must be a simple theory.
I thought, as I am quite a logical person that i would take more naturally I'm not going to give up at the first hurdle, as this quite literally is.
and I'm 25 years old I'm not a school kid trying to cheat on homework, once i am told the theory I will work on actually understanding how it functions and its other uses.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to go back over the past replies and read them carefully. There are a number of them that can help you break through the mental block you're having right now.

You actually posted some "idiotic" (your words) code that if you look for a pattern, you'd realize that it's exactly what a for loop does. Specifically, look for a pattern of consistent change from one case to the next.
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You might want to go back over the past replies and read them carefully. There are a number of them that can help you break through the mental block you're having right now.

You actually posted some "idiotic" (your words) code that if you look for a pattern, you'd realize that it's exactly what a for loop does. Specifically, look for a pattern of consistent change from one case to the next.


Thank you Junilu, and everybody else, a little more careful mapping, got me from where i was 1hr ago:

to where i am now:

Just a little tidying, but i feel i was so close all along just skirting the edges.
Thank you very much guys appreciate all the help (although my brain is trying to leave and find a quiet corner to hide in now lol)
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing you might want to try: take small steps. Don't try to solve the whole problem all at once.  You can reduce this to a simpler problem first, then once you've solved that, introduce a little bit more and adjust your solution.

Here's one way to do that:
1. First solve the problem of printing a word as many times as it has characters.  So a word that has 1 character in it will print only once. A word with 2 characters should print twice, 3 characters 3 times, 4 characters 4 times, and so on. I think you actually already have the code to do that. 

In solving this, your expected output would be:

2. Once you have that first part working, then add another requirement: Print X number of spaces before printing the word. Since you already have working code from solving the first part of the problem, you can just take that code and modify it a little bit to add the "Print X number of spaces before printing the word" requirement.

Describe what needs to be done. Being able to clearly describe the problem is half the solution. For the first line, print 0 spaces, second line 1 space, third line 2 spaces, fourth line 3 spaces, fifth line 4 spaces, ... and so on.  Write down how the iterations would progress and all the values you think you'd be dealing with. Try to find change patterns and repetitions. See how these numbers and their correlations can be used to control what your program does.

3. It also helps if you have descriptive variable names. Single letter variable names may be convenient to type but they don't help you think about the problem. A name like "x" or "a" is too abstract and your brain has to work overtime to keep translating what "x" or "a" really mean in the context of the problem you're trying to solve.  Try names like "numberOfSpaces" or something that you can easily relate to the context of the problem. Don't make your brain work harder than it needs to.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stef Aucky wrote:
to where i am now:

Just a little tidying,

You really have to work on formatting your code properly. If you're using an IDE like Eclipse or NetBeans, it's a simple keyboard command: CTRL+SHIFT+F 

Use it and use it often.

See how proper formatting makes the structure of the logic much clearer:

Now, use better names and add some white space and your code becomes more expressive and easier to read:

Now, read that again and ask your brain if it wishes you hadn't put it through so much trouble before... 
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priceless good amount of advices by Junilu. I could add only little thing. Try to employ methods.
One of iteration parts you have, you can nicely wrap to a method to print a number of spaces based on the argument (number of needed spaces) supplied.

That would add more clarity to the whole picture. An example:
Thinking to the last detail could make you proud of your program so you'd want to show to your friends what you engineered over the past few days.
Code formatting, identation are tremendously important techniques. You need to get it right in all cases, right from the beginning. Quite often poorly formatted and indented code, same as poorly chosen variable names are the main reasons not letting to see a solution.

[I was referring to Junilu's post one before the last one. Haven't seen the most recent one upon writing mine]
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here's the one-loop version I was alluding to previously:

If you use a nice feature of for-loops that allow you to have multiple increment expressions, you can make that even shorter:

 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach:
You know, that you will need maximal a string with s.length() - 1 whitespaces prepended, thus you can create this string at the beginning and then simply re-use sub-strings of it:
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:And here's the one-loop version I was alluding to previously . . .
That has the problem that you are using += on Strings, which we all know can lead to slow performance. I shall think up a one‑looop version which doesn't use +=.
If you use a nice feature of for-loops that allow you to have multiple increment expressions, you can make that even shorter: . . .
I presume you are using the word nice in its ironic sense. If we are going to go into 1970s C‑programming style, I challenge you to write that loop with an empty body
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And let's see if we can't take the print instruction out of the loop:-If you miss out the strange arithmetic to calculate the size of the StringBuilder and just write new StringBuilder(), nobody will notice any difference. The System#lineSeparator() method was introduced in Java7. I used print rather than println because the StringBuilder ends with a line end sequence.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've known me long enough that it shouldn't surprise you when I say that I don't give much thought to little optimization worries like += with strings. It's a small enough program.

Even with my production code, I'll think nothing of something like that unless I know it will be executed thousands of times per second. I just simulated 1900 generations of an 80x40 Game of Life grid using string concatenation and the display never even so much as blinked (well, OK, it did "flash" a few times as the display scrolled). The output to console was so fast I had to put a Thread.sleep() of 1/5s so I could appreciate the animation effect of the patterns the evolving population was going through. After seeing that kind of performance, I really can't worry about spaces being concatenated what, a few hundred times max maybe? How long is the longest word you'd expect to run this program with?

I was actually not trying to be ironic/(sarcastic?) with "nice"; I thought that being able to put that statement/expression in the loop header was a good thing. It doesn't really make the code harder to read, IMO, even though I know there are many programmers who might still need to educate themselves about that capability of for loops. Why, do you think it's bad?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But just to so OP knows: Campbell does raise a valid point: think twice about doing String concatenation inside long-running loops. It can be a problem if you need to have good performance under heavy load. I don't want anyone to get the impression that I just blow off any concerns about performance; I just don't worry about it too much if the price to pay for a negligible gain in performance is outweighed by a high cost of code readability. Performance has to really suck before I am willing to sacrifice readability.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. You won't notice the detrimental effects of += until your iterations get into five figures.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . . Why, do you think it's bad?
It reminds me of the way the older C books recommended writing loops. Now I have had another look at the loop, it is not as bad as I thought originally. My apologies.
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:


I really like your solution! - still two minor nitpicks (as one of your reasons was performance):
The initial length should be 'text.length() * (text.length() + System.lineSeparator().length()) + text.length() * (text.length() - 1) / 2'.
The space builder could use append(char) instead of append(String).

A significantly faster alternative would be to directly use a char-array, below approximated times:

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:
A significantly faster alternative would be to directly use a char-array, below approximated times:

Not that I don't find your NASA Apollo 13 Mission-like dedication to squeezing every last nanosecond of performance out of your code admirable and awe-inspiring but ... really? You want to trade something like

for this

just to save 8500ns and some change for a String that's just a little over half a tweet long? Yikes. Even a Raspberry Pi can handle that much space += " " without breaking a sweat.

Just sayin'  ... ¯\_(ツ)_/¯

 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Not that I don't find your NASA Apollo 13 Mission-like dedication to squeezing every last nanosecond of performance out of your code admirable and awe-inspiring but ... really? You want to trade something like
...
Just sayin'  ... ¯\_(ツ)_/¯


No, I wouldn't want to trade it, but assuming that I had a bottleneck and had to change it (or would write it as a general utility method), I would aim for the fastest implementation possible and thus would switch from String directly to char[] rather than to StringBuilder ;)
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been playing around with Go and there was this code from the sample implementation of Conway's Game of Life:

Line 10 is my addition so that the display would even out horizontally. It's a little cryptic but the equivalent of my Java toString() implementation is:

I didn't do any benchmarks but just from a human perceptible POV, there wasn't any difference in performance.  Campbell, when I said I tried this with 1000K + iterations with String concat, I really did and there wasn't much perceivable difference in performance although I'm sure the string concat version was far less efficient that this version here. Given that this code is no more complicated or difficult to read than the string concat version, I opted to go with this one.  It's mesmerizing to watch the game evolve over thousands of generations. I could make it my screen saver.  I'm working on refactoring the code to allow for a graphical display to be plugged in to the base code.

If anyone is interested, you can see the original Go code here: see https://golang.org/doc/play/life.go

I'll post my Go and Java versions on Github over the weekend.  The Java version is 153 LoC including about 40 lines of JavaDocs.

Now that I look at that revision I made to the Go code, I realize I should have done this instead:

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:. . .
The initial length should be 'text.length() * (text.length() + System.lineSeparator().length()) + text.length() * (text.length() - 1) / 2'.
. . .
I was almost guessing about the arithmetic. As long as the StringBuilder is longer than its maximum contents, you shoul‍d get good performance. There might be a slight delay if the StringBuilder has to be enlarged. At least I beat the concat method.
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last report from my NASA Apollo 13 mission :

I tried once again a modified version and got even faster results, shows pretty well that there is nearly always room for improvements (even though the improvement is hacky as it just avoids the copying of the array, thus has the same absolute effect on each implementation (e.g. -5000ns for length 78)):
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:...got even faster results, shows pretty well that there is nearly always room for improvements...

Since we are in the Beginning Java forum, the readability is the only improvement needs to be taken seriously!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!