Results 1 to 17 of 17

Thread: wont print zero

  1. #1
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default 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 12:17 AM.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, it would also be much easier to get a string and loop through to reverse it.

  4. #4
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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.

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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).

  6. #6
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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.

  7. #7
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    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.

  8. #8
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    would you mind dumbing that down a bit or a pseudo code?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jjth39347 View Post
    would you mind dumbing that down a bit or a pseudo code?
    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);
    }
    The code assumes that input isn't null and you still have to check whether or not its value forms a valid int value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default 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

  11. #11
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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);
    }
    The code assumes that input isn't null and you still have to check whether or not its value forms a valid int value.

    kind regards,

    Jos
    we aren't allowed to make our own methods at this point in the class, I understand your code and like it just my teacher wont accept it.
    Last edited by jjth39347; 03-26-2011 at 11:53 PM.

  12. #12
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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

  13. #13
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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!

  15. #15
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    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.");
    	}
    }

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  17. #17
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    I wish I could "like" your comment pbrockway, cause its defiantly true.

Similar Threads

  1. Why Wont This Work???
    By Billywizz in forum New To Java
    Replies: 11
    Last Post: 03-09-2011, 02:33 AM
  2. why wont the %.2f work here?
    By jjth39347 in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 05:55 AM
  3. Able to compile OK, But wont run..Help
    By subiedude101 in forum New To Java
    Replies: 2
    Last Post: 02-20-2011, 08:37 PM
  4. Why wont this compile?
    By Student101 in forum New To Java
    Replies: 8
    Last Post: 11-18-2010, 05:33 AM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •