This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Breaking an integer into individual digits.  RSS feed

 
John Guilliani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out how to read in an integer and break it down into a sequence of individual digits in reverse order. Example: an integer input of 12345 gives an output of
5
4
3
2
1
Any help will be greatly appreciated.
Thanks in advance,
John
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John
How about following,

Here I have just included,
input = Integer.parseInt(args[0]); to simulate that we are actually dealing with "int" value. Otherwise, we can directly say,
String strInput = args[0]; you know...
Regards
Maulin
 
John Guilliani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maulin, I see what you mean, but the exercise asks me to use the following layout:

Then call System.out.println(myExtractor.nextDigit()) five times.
Any suggestions?
John
[ October 11, 2003: Message edited by: John Guilliani ]
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ October 12, 2003: Message edited by: Dirk Schreckmann ]
 
pk kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John
Hope this helps you

Regards
pk
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ October 12, 2003: Message edited by: Dirk Schreckmann ]
 
John Guilliani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works, Thanks a bunch.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John
A couple of points. Firstly, beware of interpretation of specifications! When your instructions say 'in reverse order', they could mean one of 2 things:
- Reverse order according to the ordinal position of the character in the original number, e.g. 143562 becomes 265341 (the code you got from the previous post will do this for you)
- Reverse order according to the cardinal value of the digit, so 143562 becomes 123456 (the code you have will not do this). The fact that you have been asked to write code to operate on only integers makes this a possible interpretation.
Now, you can handle this with different approaches, depending on which of the above is required.
This leads me to my second point, which is that it is probably much better for you at this point to get used to working through a problem and creating an algorithm to define what needs to be done to solve it. This is the basis of all programming that you will do in the future, so its better that you get some practice on how to do this, rather than just taking somebody else's code and using that. Give a man a fish Vs teach a man to fish - you need to know how to fish.
So, how do you catch this fish? Well, for case 1, you do not actually need to regard the String as a number at all - the same algorithm can (and should) be used to reverse the order of the charaters in any String (i.e. you can apply the same algorithm to both '143562' and '143t?@').
So, you would need to create a new object (String, StringBuffer, even char[]), empty at first, to hold the characters of the new 'reversed' String, and populate it by looping through the characters of the original String, starting from the outermost character, and working towards the beginning of the String, adding each new character that you find to the object you created. Try to visualise this - it is the articulation of the way you would do this naturally with a pen & paper if somebody gave you the String '143562' and asked you to write down its characters in reverse order.
I will not give you the code to do this, rather I will tell you that you need a for loop to iterate through the String, and you should look at the String class, in particular the methods charAt() and length().
Now, your instructions specifically tell you that it is an integer that you will be doing this operation to, so that suggests that you may need to do more work, even if you only need to reverse the order of the characters. You will still need to do some conversion from an integer to a String (luckily this is very easy).
If you do need to sort the individual digits into reverse order (for the second interpretation of the instructions), then that involves more work, so I won't go into that right now. But it would be a good exercise for you to think about how you would go about solving that problem.
A good place to start is to think about the steps you go through unconsciously to perform tasks like these manually. You then have the basic algorithm - the next job is to convert it into instructions that the machine can understand and execute.
I hope you can understand why I made this post - any number of possible solutions could be posted in a few lines of code, but I don't believe this would help you to become a competent programmer. You need to get to the point where you can write this type of code yourself, so focus on the skills you need to be able to do this, rather than just the code. Perhaps try to come up with your own code to solve this problem - the process involved in doing that will be much more valuable to you than the code you now have.
Good luck
Michael
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although I know that you guys are trying to be helpful, just giving the answer to an assignment is not really helping. When I give an assignment I want to see if a student can do the work. If they can't then that tells me about the work that I need to do with the student. It's fine to give advice but giving the complete solution isn't really helping anyone.
 
Michael Fitzmaurice
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas
Agreed. That's why I won't post code for this type of question. I tried to point the way without giving the solution. Do you think I gave too much information? I was trying to leave room for John to learn by doing. Hopefully he will go and write his own code.
Michael
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
... It's fine to give advice but giving the complete solution isn't really helping anyone.

That's why I tell my students up front that they can find all the help they need on the Internet up to and including having someone do their homework for them (see RentaCoder.com).
The kicker is that THEY will have to produce on the final since it's a practical and worth 50% of their grade.
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Fitzmaurice's post was excellent, and should enable you to solve your problem. I just wanted to add to Michael's point about the input. Depending upon how you get the input to your program (i.e. asking the user to input a value), you need to think about and understand what you are getting as input. You may have to do a conversion as Michael suggests, but depending on what you use to ask a user for input, you may get a String in which case no conversion would be necessary. I hope this doesn't just confuse the issue....
 
BishalG Ghimire
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!