• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Separating the Digits in an Integer

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you have so far?
 
Mike Atoms
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mike
I think you have tried the program and hence i give you solution.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe something similar to this

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though this seems an old topic I got the same assignment and took care of it this way.
(I won't be posting the entire code, just my methodology, in order to not ruin the excercise for future students.)

My method: Manually split the number by dividing it by powers of 10, (starting from the highest power of 10) and deduct that number from the integer before repeating the same method all the way down to 5 separate numbers
Advantage: more compact code, less variables to declare
Disadvantage: not a scalable solution (if you were to get the same assignment but instead with an integer existing of a thousand numbers this code would become unreadable)
[Extra note: the method is scalable however if you write it recursively. Because recursive coding is something I think someone struggling with this assignment will not have grasped yet, I did not include it in this post.]



e.g.
1) num5 = 12345/10000 = 1
2) num4 = (12345-1*10000)/1000 = 2345/1000 = 2
3) num3 = (12345-(1*10000+2*1000))/100 = 345/100 = 3
4) num2 = (12345-(1*10000+2*1000+3*100))/10 = 45/10 = 4
5) num1 = (12345-(1*10000+2*1000+3*100+4*10)) = 5

(Obviously the last number does not need to be divided by a power of 10 because it is always a number smaller than 10.)

Hope this helps.
Greetz,
Faucillon
 
Marshal
Posts: 80872
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would prefer a method with a loop, because it would be scalable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic