0

first sorry about my English(not my native language). I am new in programming (currently learning Java) and just finishing lecture about looping. I had a task to reverse random number from 1 to 9999, and got stuck with a bug zero:

example: 23100 output:132 and solution is 00132

Since I still don't know Arrays, convert to String(manipulation),object solution etc…. I couldn't find beginner solution for this problem.
Since this page helped me a lot, I decided to, maybe help someone: this is beginners solution to problem:

123 reverse 321

12300 reverse to 00321 // bug problem with zero solved

now I am still stuck with problem : 00123 and output 32100 not 321 but hope solve this soon

Best regards

import java.util.Scanner;
public class MP{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("enter number:\n");
int x=input.nextInt();
int temp=x;
int z;
    while(temp>0){
    z=temp%10;
         if(z==0){
            System.out.printf("%d",0);
        }else{
            System.out.printf("%d",z);
        } 
    temp=temp/10;
 }}}
darko taom
  • 19
  • 2
  • Voting to close as *off-topic*, because questions about programming do not belong on Super User. See the [help center](http://superuser.com/help/on-topic) for details. This question should be migrated to [so] soon; please do not cross-post, as this will happen automatically once enough close votes have been cast on this question. To ensure you retain control of the question, please create an account at [so] and associate it with your Super User account. – bwDraco Feb 22 '16 at 17:47
  • 00123 is not an integer it is hexadecimal number. – SmashCode Feb 23 '16 at 01:46
  • the output you had given it is coming is also wrong it will come as 28 as it is hexadecimal number – SmashCode Feb 23 '16 at 01:47

3 Answers3

0

Well whenever you try to store 00123 as an integer it is stored as 123 [ EDIT: apparently Java assumes that those leading zeros means that you are inputting a hexadecimal number (base-16 rather than base-10) so the result will be not 123, but 291]. So then when you reverse it, the leading zeros are left out. It seems to me that the only way to do what you are trying to accomplish would be, unfortunately, to use either arrays or Strings (I would recommend using a String).

That being said, if you are avoiding using a String/array simply because you don't know how to use them, then have no fear; Strings are fairly easy to use. A problem you might be running into is that people tend to not make their code very easy to understand. If that is the case, we share the same pains. I will try to make an example that is easy to understand:

It would look something like this:

import java.util.Scanner;
public class MP{

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("enter number:\n");
        String temp = input.next();      //use next() to look for a String

        int digit = temp.length()-1;     //starting at the last digit
        while (digit >= 0){
            char z = temp.charAt(digit); //get the current digit
            System.out.print(z);         //Print that digit
            digit = digit - 1;           //Go backwards one digit
        }
    }
}

Eventually you should be able to write a much shorter program for the same thing:

import java.util.Scanner;
public class MP{
    public static void main(String[] args){
        System.out.print("enter number:\n");
        String temp = new Scanner(System.in).next();
        for (int i=temp.length()-1; i>=0; i--){
            System.out.print(temp.charAt(i));
        }
    }
}
Jonah Haney
  • 521
  • 3
  • 12
  • You may want to read this http://stackoverflow.com/questions/565634/integer-with-leading-zeroes – OneCricketeer Feb 23 '16 at 02:00
  • @cricket_007 Oh wow I did not know Java assumed that leading zeros indicate a non-base-10 number. Of course I think his question merely deals with the fact that the leading zeros are not preserved, so he cannot successfully reverse the number entered without a sort of "loss of precision" as you may call it. I will edit my answer to address that – Jonah Haney Feb 23 '16 at 03:33
0

Since you are the beginner why don't you work with String as with String? It should suit your purpose:

import java.util.Scanner;

public class MP {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("enter number:\n");
        String x = input.next();
        for (int i=x.length();i>0;) {
            char c=x.charAt(--i);
            if (c<='9'&&c>='0')System.out.print();
        }
        System.out.println();
    }
}
Alex
  • 4,457
  • 2
  • 20
  • 59
0
  String lm = "00123";
  StringBuffer bn = new StringBuffer(lm.replaceAll("^0+(?=\\d+$)", ""));
  lm = bn.toString();
  System.out.println(bn.reverse());

I did for strings it will not when we convert int to string as given is in hex format it will take 38 into string as 00123 is equivalent to 38 in integer.Hope you like my work.

Happy coding.

SmashCode
  • 741
  • 1
  • 8
  • 14