• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java : How to reverse an inputted number  RSS feed

 
Kawita Kandpal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning Java and need to code a program that will reverse an inputted number.

As I understand, the program should prompt the user to input a number, and then the program will output the the numeric reverse of that number. I am working my way through "Head First Java" and am feeling overwhelmed.

What I understood:

1.
Since there is an input value and output value, importing a "java.util.Scanner" is required.

2.
(2) println statements are required:
-One println for input.
-One println for output.

3.
-An int for the number the user inputs is required.
-An int for the reversed number is required.

I am struggling with how to convert the aforementioned English into Java code. I warmly welcome your thoughts.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop thinking about what you NEED, and think about what you want to DO. Programming is about thinking.

So start of with general statements of what your program should do. For example:

1) ask user to input a number
2) get the number
3) validate it actually is a number (optional)
4) compute what the number is in reverse
5) print out the reversed number


Once you break down the problem like this, then try to get ONE PART working. #1 might not be too hard. Make it a method that does only that, then call that method.

Once you've done that, you may want to do #5 next - again, as it's own method. Have it print out a dummy value at first. then tweak it to print a number you pass it. Once it is working, try adding in #2. Once that works, pass that value to your method you wrote for #5...and so on.

you want to write your code in teeny, tiny pieces, testing them THOROUGHLY along the way.
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to get help is to show us what you have done so far. Have you written any code yet? How about you start with a very simple 'hello world' program that you can compile and run successfully?

Already beyond that stage? Great! How far beyond? Show us.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kawita Kandpal, a warm welcome to the Ranch

First of all, read that FAQ written by one of CodeRanch moderators. Basically it will explain you the classic situation which you're facing it now.
After you done, I see you feel kinda, "yeah, I've been trapped too".

I'd suggest start by writing pseudo code in plain English, or other your native language, without using that technical terminology you used in your first post - I have in mind all those details about: int, System.out, Scanner.
Try to solve that in in plain text, so you'd be sure, you fully understand the problem i.e:

1. Ask user for number
2. Reverse number
3. Show reversed number

For the first round it is enough to have an idea, what is the task about. After you got that, revise those steps and dig deeper to each of them by providing more details. So next iteration of steps would look similar to:

1. Print message by asking the user input for number
2. Store user input
3. Reverse number
4. Print reversed number

See, you got more details now, you know, that before you reverse number, you need to store it somewhere first. But it is not fully clear yet other parts, how to reverse it. So keep going.

1. Print message...
2. Store user input...
3. Create variable for reversed input.
4. Write an idea this time, HOW you're going to do that (don't use exact terminology, are you going to use for, while or do-while loops, maybe you going to use modulus, maybe division - not that important exact details)
5. ...

Give a try, and see if it is getting clearer, what is your actual task is. I'm sure you'll enjoy the process you actually start solving your problem, by defining the steps, later refining them - that is the all beauty about programming
 
Kawita Kandpal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, Tim, Liutauras, I deeply appreciate your direction!

Fred, your advice of creating general statements of what needs to be done in single steps and then getting each step to work eliminates the overwhelm. I actually enjoyed approaching the coding process.

Tim, I have a portion of my code for consideration. Please see below. I am at the step of how to compute the number in reverse.

Liutauras, I appreciate your warm welcome and wow I wish I started with FAQ 'What Not How' because that would have placed me on the correct path. Since I am on the step of how to compute a number in reverse, I really like your recommendation of not employing the exact terminology for this step, rather how I'm going to do the step in plain English.

At present, I don't actually know how to compute a number in reverse, so I've got a date with Google.

Please accept my apologies for all of the code comments...

Thank you all


 
H Adusumilly
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Firstly I am sorry for posting my question in this thread.

I have a question I need answered while creating constructors. I am trying to post a question regarding the same, starting a new thread for the topic. Though I am trying to submit it, the question does not get posted. Can you help me know how to post the question?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kawita Kandpal wrote:
At present, I don't actually know how to compute a number in reverse, so I've got a date with Google.


Another way to think about it is that when you reverse a number you're really just reversing the representation of that number (the Arabic numerals). So really you don't have actually have to do any mathematical computation at all you can just manipulate a String. That can actually be achieved with just one line of code (not through String class though). You still might have some work to do though depending on what you're allowing for valid input/output. For example, will the reverse of 4000 output 0004 or just 4? How do you want to handle a negative sign?
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Kawita Kandpal wrote:. . . Please accept my apologies for all of the code comments... . . .
Apologies accepted. I am afraid I am going to be hard with you anyway. Sorry. But those comments really are unnecessary. Look at this sort of thing:-That doesn't help anybody. Those comments should all be removed. Comments should explain what is not obvious from reading the code, or documentation comments should explain to the user how the code works. This sort of thing would be better:-To te best of my knowledge that Ritchie's approximation doesn't exist; I simply pulled some figures out of thin air.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!