• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to learn Java thru studying a booklet. I came across this problem from the book that has really confused me.

Write an application that inputs one number consisting of five digits from the user, separates the number its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

4 2 3 3 9

[Hint: You will need to use both division and modulus operations to pick off each digit]

Assume that the user enters the correct number of digits. What happens when you execute a program and type the number with more than five digits? What happens when you execute the program and type a number with fewer than five digits?

I do not understand how the division and modulus operators can make this problem have 3 spaces between the 5 numbers input. I know how to use inputs but this problem has me confused. Can someone please help me in the right direction?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The division and modulus are not there to get the three spaces, but rather to obtain the individual digits from the number. If you had the integer 42339, how do you get the number in the hundred's position? You could convert it to a string, get the third-from-last character, and convert that back to a number. But what if your number was 43? You'd have to write in the logic to check for out-of-bounds exceptions.

With division and modulus, you can get the number without any extra checking. Exactly how to use division and modulus is left as an exercise to the reader, as I suspect that that is the point of the exercise.


As an aside, if I were doing this problem, I'd convert the number to a string and then print out each character spaced with three spaces. Since the solution is non-mathematical, that would suffice. However, I suspect that the goal is see how division and modulus work, so follow that approach.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get started print 42339 % 10 and see if the result looks useful in your problem.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic