• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reversing numbers  RSS feed

 
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone I am having a bit of a problem in my coding here. I am writing a program to reverse a user inputted number (Example - 54321 is 12345) The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero. Would it make more sense to just set this up using strings?

 
Marshal
Posts: 4052
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading the input as an int will drop any leading zeros. So 01234 is stored as the int 1234, which is as you would expect it to do. If you want the leading zero then you should read it as a String "01234". You will of course have to think of another way to reverse the characters.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero.

Also, if you enter a number which ends with a zero the output will not start with a zero. You could handle these special cases in your existing code but special case code is often a sign that the technique used to arrive at the solution is flawed apart from which it's probably easier to just treat the whole thing as a string and reverse the string.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:You will of course have to think of another way to reverse the characters.

You might find the static method Integer.parseInt(String s) handy.
And if you decide to reverse the string (before it become an int) then the StringBuilder class has a useful reverse() function.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derik Davenport wrote:And if you decide to reverse the string (before it become an int) then the StringBuilder class has a useful reverse() function.

True but unfortunately if this is homework then I'd imagine using StringBuider won't be allowed and if the OP's doing it to improve his/her skills then using reverse() only teaches you to read the API docs (which isn't an altogether bad lesson I suppose).
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my reverseDigit method to this, but I am still receiving the same results. What am I doing wrong here?

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method takes and returns an int so it will lose any leading zeros, you need to keep the number as a String.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difficulty you are having is that the zeros are lost from your input as soon as you call nextInt on your console object.
Put another way, inputNum does not have the zero that you need. No matter what you have in reverseDigits(), it can never reconstruct those lost zeros. You must capture them and preserve them.

You need to change console.nextInt() to something else.

String inputString;
inputString = console.next();
inputNum = Integer.parseInt(inputString);


Now you can pass inputNum and the inputString (or at least a number representing inputString.length) into the reverseDigits method. If the final result of reveseDigits() doesn't have as many digits as inputString has characters, then you know that there were leading zeros.

Alternatively you can just pass the string in by itself, and then have reverseDigits() create the variable you call number from that.


Tony wrote: unfortunately if this is homework then I'd imagine using StringBuider won't be allowe
I didn't assume that this was homework and not self learning. But since I couldn't rule that out I also suggested the Integer.parseInt() function. With that he can solve this with minimal changes to his reverseDigits() method.



 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derik Davenport wrote:Now you can pass inputNum and the inputString (or at least a number representing inputString.length) into the reverseDigits method. If the final result of reveseDigits() doesn't have as many digits as inputString has characters, then you know that there were leading zeros.

No you don't. You would know there were either leading or trailing zeros but you don't know which.
You would need to test to see if the input number was less than the string length to determine if there are leading zeros and then if the length of the reversed number was shorter than the length of the input number to find trailing zeros. For example what would happen if the input was 00600.
 
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you are learning about stacks in your class nw. So, this is a usual question that we got to solve using a stack. But i haven't used a stack here.

public static String reverseDigit(String input){
char[] nums=input.toCharArray();
StringBuffer b = new StringBuffer();
for(int i=nums.length-1;i>=0;i--){
b.append(nums[i]);
}
return b.toString();
}
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Boynton wrote:Would it make more sense to just set this up using strings?

Probably, if you've been specifically asked to retain every digit of the original "number" (which, in fact, isn't a number at all - it's a numeric code or string).

Then, as everyone else has said, the problem becomes one of simply reversing the characters in a String.

However, if you're also supposed to verify that all the characters in that String are numeric, then you have an additional check to make - which you can find in the Character class.

HIH

Winston
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have nothing to add to what others have already suggested. I do want to point out that you are one of the rare "beginners" who formats their code properly and uses good, meaningful names, and uses methods to break up a large task into smaller, simpler ones. Good job!
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the feedback, everyone! I will give it a try and see what I can come up with. I will post my successful code when I am done.
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I redid all my coding, although it was easy to stay with working with int's I converted everything to strings. Here is my final code! Thanks again for all the help everyone!

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But now you can enter the number "Jeff" and have your program return "ffeJ", right? So what's all that about reversing a "number"? Why do you accept things which aren't "numbers"?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting your solution.

BTW the following line is not necessary, Strings are immutable so you don't need to create a new copy to assign to reverseDigit, in fact you don't even need reverseDigit.
Also reverseDigit is a poor choice of name as it doesn't describe what the variable represents.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony's and Paul's valid points aside, I think you made a good run at the problem. Here are a few things to think about:

1. You can turn a String into an array of characters and iterate through that instead of using charAt(). The code will be slightly more straightforward and you'll save a method call.
2. As you iterate through the char[], you can use Character.isDigit() to check for digits vs non-digits and throw an exception if the input is invalid
3. Instead of reversing the order of iteration (from high index to low index), you can reverse the order in which you concatenate the next character to the result String.

Bonus points if you can implement another version in accordance to this.
 
Jeff Boynton
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. This was all for a class assignment. I am still very new to this and am slowly learning as I go.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you weren't writing a class assignment then the way to reverse a String is to use the reverse() method of StringBuilder rather than writing your own code. But having beginners write this sort of code is part of the learning process.

There's also a lesson about sloppy specifications here... what is a "number" anyway? Like you said, what happens if the user puts a zero at the front of the number? Does that count as part of the number? In real life when you get specs which are ambiguous like that, you have to go back to the person who gave you the specs and try to pin them down. Guessing is a risky approach, because you might guess wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!