This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Separating the Digits in an Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Separating the Digits in an Integer" Watch "Separating the Digits in an Integer" New topic
Author

Separating the Digits in an Integer

Mike Atoms
Greenhorn

Joined: Aug 29, 2009
Posts: 12
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

Joined: Sep 29, 2008
Posts: 12617

What do you have so far?
Mike Atoms
Greenhorn

Joined: Aug 29, 2009
Posts: 12


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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 29, 2009
Posts: 12
Thanks for the repost, I won't make the mistake again. I am a newbie!
Mike Atoms
Greenhorn

Joined: Aug 29, 2009
Posts: 12


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

Joined: Sep 29, 2008
Posts: 12617

You already know how to print--give it a shot.
Mike Atoms
Greenhorn

Joined: Aug 29, 2009
Posts: 12


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

Joined: Aug 04, 2009
Posts: 125
@Mike
I think you have tried the program and hence i give you solution.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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.


Steve
Mike Atoms
Greenhorn

Joined: Aug 29, 2009
Posts: 12
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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Maybe something similar to this

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Separating the Digits in an Integer
 
Similar Threads
comparing digits in a number
finding the number of digits in an integer
formatting, 0 and #
number formatting????
Using an int to hold a four digit date