This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Problem i cannot solve  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I am a beginner in Java programming language and i stumbled upon a problem i cannot solve.It goes like this:
Write the DN02 program , which will receive the words received in the arguments, framed and displayed on the screen. Example: when calling a program:
java DN02 That's it!    let the program print:
*******
*    To  *
*    je  *
*    to! *
*******
The text that the program prints in the box must be aligned centrally (shorter words must be aligned in the middle of the space).

As you can see in a given picture, i cant make the right side align the right way.Maybe i should solve it with string formatting but i dont know how.Can someone help me?
Thanks
Screenshot_2.png
[Thumbnail for Screenshot_2.png]
 
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Unfortunately there isn't a straightforward centred option for printing to the console. I am afraid you are gong to have to count characters in your words and calculate the spacing. Let's imagine you have a nine‑letter word to centre in 20 keystrokes' wdth. Use one places for the left *, and that leaves 19. Subtract (9 + 1), the 1 being for the right *. That leaves 9; half of 9 is 4, so try printf:-Alternatively, create a StringBuilder object and append those spaces to it.

You will need to expand my code to get it to work.
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is too complicated for you, look at this Java™ Tutorials page.
 
Predrag Djindjic
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Unfortunately there isn't a straightforward centred option for printing to the console. I am afraid you are gong to have to count characters in your words and calculate the spacing. Let's imagine you have a nine‑letter word to centre in 20 keystrokes' wdth. Use one places for the left *, and that leaves 19. Subtract (9 + 1), the 1 being for the right *. That leaves 9; half of 9 is 4, so try printf:-Alternatively, create a StringBuilder object and append those spaces to it.

You will need to expand my code to get it to work.




But the problem is,i have to make a for loop that will do that for any given word.For example,if I type "Java is cool", the biggest word has 4 characters,the width is the length of that word +4 because i have to give it a space on each side and that a '*' on each side too.So for every next word the spacing can be calculated by (width-current word)/2 but after that i cant align the remaining star.Look at my code at given picture and tell me if I messed something up.
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't read the code in the screenshot; please post it as text.
 
Predrag Djindjic
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you were right to iterate the args array to find the longest String. I think there is a problem with arithmetic, remembering that in integer arithmetic, 2 / 2 == 3 / 2. So your arithmetic has difficulty distinguishing the length of To and To!
Your ifs in lines 10‑12 look complicated, so I haven't been through them looking for errors. But such a complicated formula is likely to be error‑prone.
 
Marshal
Posts: 6595
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Predrag, welcome to the Ranch.

Even though the problem isn't complex, but still you could make your life easier by decomposing it to smaller problems, so each of those you could solve with bunch of small methods. That way would be easier for you to test each of those methods individually and see where the logic flaws.

Don't forget to validate whether command line arguments were passed to your program, and if not - handle it appropriately.
 
Liutauras Vilda
Marshal
Posts: 6595
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

earlier, I wrote:decomposing it to smaller problems


For example: findMaxLength(); isPictureArea(); isWhitespaceArea(); isFrameArea();
 
Sheriff
Posts: 13187
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming problems like this are easier to solve if you break it down into smaller problems. There are several smaller tasks that I can see. Finding the longest string is one. Centering a string with an odd number of characters is another. I think you can find others.

Don't try to solve everything at once. Solve small problems and put the individual solutions together to solve the big problem.
 
Predrag Djindjic
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies, I solved it with string formatting.
 
You can't have everything. Where would you put it?
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!