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!!!
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.
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
posted
0
Thanks for the repost, I won't make the mistake again. I am a newbie!
Mike Atoms
Greenhorn
Joined: Aug 29, 2009
Posts: 12
posted
0
Here's what I've come up with so far. I now need to print each digit separated by three spaces. Please assist.
"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
posted
0
@Mike
I think you have tried the program and hence i give you solution.
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
posted
0
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!