Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Separating the Digits in an Integer

 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am newbie taking first class in JAVA. Can someone help get me started. I need to write an app that allows user to input five digit integer. Then the app must split the integers into there individual digits separted by three spaces using say a print method. Please help!!!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far?
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I know that I need to first check that the user inputs a five digit integer and then I need to divide the integer by 10000 in the first step to strip off the digit and then use the mod to keep the remainder and then divide that by 1000 so on and so forth. The syntax is still confusing to me.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. Unformatted code/config/etc. is difficult to read. You can edit your post to include them using the button or re-post the question with proper formatting.

(I've edited your post this time.)
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the repost, I won't make the mistake again. I am a newbie!
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here's what I've come up with so far. I now need to print each digit separated by three spaces. Please assist.

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already know how to print--give it a shot.
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here's what I tried but I get this error:

"Exception in thread "main" java.util.InputMismatchException: For input string: "12345"
at java.util.Scanner.nextInt(Scanner.java:2097)
at splitdigits.SplitDigits.main(SplitDigits.java:21)
Java Result: 1"

Is this from before I try to print?


 
Kabir Shah
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mike
I think you have tried the program and hence i give you solution.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurudas, I removed the solution to this problem because I don't think we should just post solutions to homework problems, it doesn't really help the OP, and it won't help other people searching the topic either.


Mike,
You get the error:
"Exception in thread "main" java.util.InputMismatchException: For input string: "12345"
at java.util.Scanner.nextInt(Scanner.java:2097)
at splitdigits.SplitDigits.main(SplitDigits.java:21)


The exception tells you at what line in your code the error happens:
"at splitdigits.SplitDigits.main(SplitDigits.java:21) "
If you look in your code line 21 will be this:


I looked up the Scanner API for the nextInt method: Scanner#nextInt(int), and to paraphrase one of my favorite movies 'I do not think it does what you think it does.' The integer you pass in to nextInt is the 'radix', or the range of values for a single digit. When you pass in a radix of 5 you are saying each digit can hold a 0, 1, 2, 3, or 4 (one of 5 different values). You don't want to use that. You just want to use the normal nextInt method without the parameter:



You have some other compiler problems, noticeably next would be that your System.out.printf() statement is not correct. I will let you see if you can find the problem.
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code: The only thing I noticed was that it needs to return the numbers in order from left to right. I managed to finally get this to work. The code is much longer than it probably needs to be. If anyone wants to try and clean it up some I would like to see how it might be done!

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a suggestion (more like observation) for you:


Remainder1 gives you the values left over which are less than 10.

One is that value divided by 10, which would be a fraction and result in 0. This isn't what you want, which is why you don't use it.

Remainder0 is Remainder1 divided by 1, which is a non-operation.

Zero us just another variable/assignment.

You can probably clean the last 4 lines of code by replacing it all with:

 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think using regex will be helpful and the code will be shorter.



I guess the spacings you can do it using the printf method or the format method or even the print method.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something similar to this

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic