This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Thank you for letting me know, I did not mean to cross the limit of asking a question.Anyway the program was my home assignment and i tried to do it. I just thought it is not necessary to write my code. I wanted it ready made !! how sad. from now on, I will undoubtly abide by the Ranch rules. please accept my apologies. thank you.
We all here to help. As Ulf suggested, post what ideas you have here, what you've tried, etc. Give us a better idea where you are stuck.
and finally, give a better idea what you are trying to do. "inverse of a number could mean several things. there is an additive inverse, a multiplicative inverse, or just reversing the digits. a better defined problem is easier to solve.
Joined: Dec 29, 2007
Fred Rosenberger, Hi, I have never written any programs before in any language,I am totally new to java. I have tried to do the program but I really need the slightest hint on how to inverse the digit given to the program.I will post my code so that you can have a look.
I know it looks awful and the logic is not correct,but this is what I have been trying to do.I think I will be using two for loops, but as I told you, i still can't apply the proper logic of inversing the digits.anyway,I am looking forward for your suggestions. thank you.
What Justin means is that you get an input, which he has maintained as a String, convert it to a char array (go through the String class API) then print out the array backwards. Find the Java Tutorial and look for arrays to find out how to do this.
I am afraid that is not quite what you have been given. You are probably told to print the individual numbers from a decimal integer using /10 and %10 operators repeatedly to get the individual digits. If you get the operators right to find the last digit, you can print it out and repeat.
That is about as much detail as we dare give you.
Joined: Jan 24, 2006
This indeed is a very simple task, whether it be the way I was speaking of, or the /10 and %10 way.
if you go to www.sun.com and go under developers->api/docs you'll definatly find what you need.
Java, in my opinion, has the best online API and documentation out of any other programming language.
I don't know what you're doing this for. If it's a homework assignment or what, but actually looking for the solution, or getting a book on java is the most help. I know it probably sounds good to just get someone to give you the answer, but when you actually figure it out yourself and it works! That's the best part of all...
If this type of problem is very difficult for you, I suggest you get a beginners JAVA book and sit and read it. The only way to get better at java is to practice practice practice... but if you try to start on the harder projects you'll never get anywhere if you don't build up to them.
But lets take a second to think about the process in which to approach a task like this. I'm going to assume that it's the string representation of a number approach and run with that...
now you need to be able to get that darn string into a character array... - look into the string class and it's many different functions - I'm sure you'll find what you need.
now you need to be able to get that character array in reverse!? - again, you did good by using a for loop. - usually you would start with i = 0, and then increment to go foward - through the array, well use that knowledge and do the opposite.
if you were needing to figure out how to do this task mathmatically, as in actually finding the last digit and stuff... You're probably just as lost as I am. - but I'm sure if you look into the process of finding the reverse of a number numerically by google searching the topic, I'm sure you'll get what you need.
I hope you find what you are looking for and succeed in getting this done.
Joined: Dec 29, 2007
Thank you both Justin fox and Campbell Ritchie you have been endlessly helpful.You have enlighten me just fine. now I will do as you both suggested.Actually, this program is my home assignment.My java knowledge is not bad but sometimes applying a certain logic to something is quite hard.The thing is that I have been thinking how can I write a function for reversing the digit given as an input.Now as i said I should work it out and if i succeed I will sure post my code here so that you can see it. thank you again, Ammar salem