• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Loop problem java exercice

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
i've been trying to do this program for a couple of days :
this is what i have to get :

for n = 4

aaaaaaa
abbbbba
abcccba
abcdcba
abcccba
abbbbba
aaaaaaa

for n =5

aaaaaaaaa
abbbbbbba
abcccccba
abcdddcba
abcdedcba
abcdddcba
abcccccba
abbbbbbba
aaaaaaaaa

this is what i was able to get for n=4 :

aaaaaaa
afedcba
aeedcba
adddcba
accccba
abbbbba
aaaaaaa

here is a pastebin so you see a bit :

http://pastebin.com/qwbxkwaS

I can't see what my problem is Thanks for your help
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Many people won't click on external links so it's generally better to post the relevant sections of code in the post (remembering to UseCodeTags (← click))
 
Demirci Ayhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, here is the code :

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And can you post your written description of how you planned to solve this problem.
 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa! This output looks interesting!
I'm gonna start working right away!!
 
Marshal
Posts: 80479
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something wrong when you have that many levels of indentation. Google for Arrow AntiPattern. The code looks like an arrowhead and the many levels of indentation suggest there is something wrong. One thing wrong is the break; statement not wrapped in an if. I don't like break; at the best of times but if it isn't wrapped in an if then I am sure it is an error. Even without reading the rest of the code.

There is also something wrong with those long lines; they are difficult to read. I have broken them so you can see how to do it correctly, and got rid of some unnecessary empty lines.
 
Ashwin Rao
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it took a little longer than I expected but I eventually got the output. The thing is you have to spend some time with a pencil and paper.
I'm not going to post the solution here (because you're not supposed to! :p) but if you're interested in knowing how I solved the problem you can contact me at: [email protected]
 
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell what happened to helpful responses?

Google Arrow AntiPattern? Really? A "Beginning Java" question and the response is to Google a pattern? If I were to venture a guess I'd say most beginners don't know what patterns are.

How about suggesting that math can be performed on a char or that an alphabet array isn't necessary? Break statements not inside if blocks is not necessarily wrong just because you don't like them. Breaks are completely legit in switch blocks.

Of course, the real question that needs to be asked of the OP is what are the requirements of the program? An example of some output has been provided, but what are the requirements? I can create multiple ways for creating the desired output but they are meaningless without the requirements.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emil Jennings wrote:Campbell what happened to helpful responses?

Google Arrow AntiPattern? Really? A "Beginning Java" question and the response is to Google a pattern? If I were to venture a guess I'd say most beginners don't know what patterns are.


Easy there, cowboy. Even if the OP didn't know what patterns are, Googling for and learning about Arrow code isn't going to hurt them.

Emil Jennings wrote:How about suggesting that ...


Again, please be careful with the way you phrase things; this comes across with a cynical tone.

In the future, try for something less snarky to make your point(s), something like the following would have come across better and more in line with the "be nice" policy we follow around here:


Out of curiosity, I looked up the Arrow code anti-pattern and I'm not sure the OP is at a place where that's going to help him/her right away. Also, I think there are valid uses for break and giving more technical reasons for avoiding them might be more useful. For instance, breaks are actually necessary in switch case statements and sometimes they come in handy in loops. These could lead us to discussions that digress from the original question though so maybe we can go there later.

Right now, it would more helpful if the OP told us what are the requirements of the program. An example of some output has been provided, but what are the requirements? I can create multiple ways for creating the desired output but they are meaningless without the requirements.

 
Demirci Ayhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all, i was finally able to do it, here is the code i used :
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job Demirci Ayhan
reply
    Bookmark Topic Watch Topic
  • New Topic