Results 1 to 17 of 17
Thread: wont print zero
- 03-24-2011, 09:09 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
wont print zero
Okay this is trying to do the reverse number but when a zero in enter in the number it wont show up in the answer.
Java Code:import java.util.Scanner; public class Lab8 { public static void main(String[] args) { //original number long number; long reversedNumber = 0; long temp = 0; //import scanner Scanner keyboard = new Scanner(System.in); System.out.println("Enter a starting number"); number = keyboard.nextInt(); while(number > 0) { //use modulus operator to strip off the last digit temp = number%10; //create the reversed number reversedNumber = reversedNumber * 10 + temp; number = number/10; } //output the reversed number System.out.println("Reversed Number is: " + reversedNumber); } }
Last edited by jjth39347; 03-27-2011 at 01:17 AM.
- 03-24-2011, 09:46 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
Ints don't have leading zeroes. You'll have to get a string from your scanner and reverse that.
Also, please use [code][/code] tags.
- 03-24-2011, 09:53 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Yes, it would also be much easier to get a string and loop through to reverse it.
- 03-24-2011, 11:31 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
sorry, I can switch it to a string but I have no idea how to use the operators I want on the string for it to still work.
- 03-24-2011, 11:49 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
You don't need any division or modulus operations. All you need is a for loop and the charAt() method of String (and possibly some way of validating that the user is only entering numbers).
- 03-26-2011, 08:27 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
I also need to add an if else statement that states if the start number and the reverse number are the same or not.... ie
start: 1331
Reverse: 1331
The numbers are the same
Start 1234
Reverse 4321
The numbers are not the same.
- 03-26-2011, 08:41 PM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 180
- Rep Power
- 0
Take an string from keyboard.Then reversed it and keep the reversed it in a new string type variable.Then compare it with first one.
- 03-26-2011, 08:50 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
would you mind dumbing that down a bit or a pseudo code?
- 03-26-2011, 08:58 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Sure, here's some real code:
Java Code:private String reverse(String input) { return new StringBuilder(input).reverse().toString(); } private boolean isPalindrome(String input) { return input.equals(reverse(input); }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-26-2011, 08:59 PM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 180
- Rep Power
- 0
String
Did you see any tutorials or books? Its a simple one.
Java Code:Scanner as=new Scanner(System.in); String S=as.nextLine(); int i; String b; b=""; for(i=S.length()-1;i>=0;i--){ b+=S.charAt(i); } if(S.equals(b)){ System.out.println("Same"); } else System.out.println("No");
If you are satisfied,don't forget to press REP for me
- 03-26-2011, 09:09 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
- 03-26-2011, 10:01 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
my program does reverse it as an inturgral number but I can't figure out how to get it to print out a damn zero such as
type in 1023
it outputs 321
- 03-27-2011, 12:44 AM #13
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
okay I caved and used string, my teacher can just get over it. It is the best way to solve this problem imo.
Thank you all, and thank you UJJAL DHAR.
- 03-27-2011, 01:34 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
okay I caved and used string, my teacher can just get over it. It is the best way to solve this problem imo.
I agree with your humble opinion. "Reversing" an integral value sounds like a string operation and indeed it is: what is being reversed is the (or rather a) string representation of the value. While it's true that you can do the reversing with mathematical operators - because of the relationship between the digits used in the decimal form of the value and the first 10 whole numbers - it makes no sense to do so when the StringBuilder class exists.
What you were asked to do is akin to opening a bottle of wine with a screwdriver. Possible, but who would bother if they had a corkscrew?
Your teacher should have said how the leading zeros should be handled. Your code reverses 120 as 21 - if the teacher wanted leading zeros he or she should have said.
As I said above you can do this with numeric operations. Basically inside the while loop you count the number of times temp is zero before a nonzero value is obtained. Then, at the end, you print that many zeros before printing the value you have calculated.
This will handle trailing zeros in the input. You might like to ask your teacher sweetly about leading zeros in the user input. If the user enters 00666 the line
Java Code:number = keyboard.nextInt();
will set the value of number to 666. getInt() effectively removes leading zeros and you have no way of telling how many there might have been. The only solution - and you want your teacher to admit this - is to read the user input as ... a string!
- 03-27-2011, 03:13 AM #15
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
yea, she is constantly looking up questions that her students ask her because she doesn't really teach java. Hell she even ask me to answer the other students questions.
but here is the final code
Java Code:import java.util.Scanner; public class Lab8 { public static void main(String[] args) { Scanner keyboard=new Scanner(System.in); System.out.println("Please enter a number:"); String number=keyboard.nextLine(); int temp; String reverseNum; reverseNum=""; temp=number.length()-1; while(temp>=0) { reverseNum+=number.charAt(temp); temp--; } System.out.println("The reverse number is:"+ reverseNum); if(number.equals(reverseNum)) { System.out.println("The given number and reversed number are same."); } else System.out.println("The given number and reversed number are not same."); } }
- 03-27-2011, 03:25 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
It's your own understanding and learning. So long as you know what / and % can do and can intelligently defend not using them here, that's the main thing.
- 03-27-2011, 03:35 AM #17
Member
- Join Date
- Feb 2011
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
Why Wont This Work???
By Billywizz in forum New To JavaReplies: 11Last Post: 03-09-2011, 03:33 AM -
why wont the %.2f work here?
By jjth39347 in forum New To JavaReplies: 2Last Post: 03-06-2011, 06:55 AM -
Able to compile OK, But wont run..Help
By subiedude101 in forum New To JavaReplies: 2Last Post: 02-20-2011, 09:37 PM -
Why wont this compile?
By Student101 in forum New To JavaReplies: 8Last Post: 11-18-2010, 06:33 AM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 12:04 AM
Bookmarks