Ali Khan

Greenhorn

Posts: 18

posted 11 years ago

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?

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?

posted 11 years ago

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.

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.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

It is sorta covered in the JavaRanch Style Guide. |