I am getting my program to compile but it will not run.

It is supposed to take a three digit number, reverse it, then subtract and add the original and reverse number.

Why is this not printing out?

Code:

    public class Rev
    {   
        public static void main(String[] args)
        {
            int num = 0;
            for (int i = 0; i < args.length; i++) 
            {
                num = Integer.parseInt(args[i]);
                reverseNum(num);
                subtractNum(num);
                addNum(num);
            }

        }

        static boolean checkDigits(int number) // checks if numbers are correct
        {
            int reverse = reverseNum(number);

            if(number > reverse)
            {
                throw new Error("Reverse number needs to be less than the original number!");
            }
            else
            {
                return true;
            }
        }

        static int reverseNum(int number) //reverses number
        {
            int reverse = 0;

            int r = 0;

            while (number != 0)
            {
                if(number > 1000 || number < 99)
                {

                r = number % 10;

                reverse = (reverse*10) + r;

                number = number/10;
                }
            }

            return reverse;
        }

        static void subtractNum(int number) // subtracts
        {
            int reverse = reverseNum(number);

            int total = number - reverse;

            System.out.println("Reverse and subtract: ");
            System.out.println(number);
            System.out.println(reverse + " - ");
            System.out.println("---");
            System.out.println(total);
            System.out.println();

            number = total;
        }

        static void addNum(int number) // adds
        {
            int reverse = reverseNum(number);

            int total = number + reverse;

            System.out.println("Reverse and add: ");
            System.out.println(number);
            System.out.println(reverse + " + ");
            System.out.println("---");
            System.out.println(total);

            number = total;
        }


}

what do you pass as arguments to your application? What do you expect it to do, and what does it do (not) ?

Member Avatar for Rahul47

I have done a few changes to your program. Hope this will serve your purpose.

public class Rev
    {   
        public static void main(String[] args)
        {
            int num = 0;
            int rev=0;
            for (int i = 0; i < args.length; i++) 
            {
                num = Integer.parseInt(args[i]);

                // Since you only wish to process 3 digit numbers.
                if(num<1000 && num>99)
                    { 
                        num = Integer.parseInt(args[i]); 
                    }
                else
                    {
                            continue ;
                    }                      
                rev=reverseNum(num);
               subtractNum(num,rev);
               addNum(num,rev);
            }
        }
        static boolean checkDigits(int number, int reverse) // checks if numbers are correct
        {
            if(number < reverse)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        static int reverseNum(int number) //reverses number
        {
            int reverse = 0;
            int r = 0;
            while (number != 0)
            {
                r = number % 10;
                reverse = (reverse*10) + r;
                number = number/10;
            }
            return reverse;
        }

        static void subtractNum(int number, int reverse) // subtracts
        {           
            if(checkDigits(number,reverse)==true){
                int total = number - reverse;
                System.out.println("Reverse and subtract: ");
                System.out.println(number);
                System.out.println(reverse + " - ");
                System.out.println("---");
                System.out.println(total);
                System.out.println();
                number = total;
            }else{
                System.out.println("Reverse number "+reverse+" needs to be less than the original number "+number+"!");
            }
        }

        static void addNum(int number,int reverse) // adds
        {
            int total = number + reverse;
            System.out.println("Reverse and add: ");
            System.out.println(number);
            System.out.println(reverse + " + ");
            System.out.println("---");
            System.out.println(total);
            number = total;
        }
}

You only need to format the output as you wish. Here is a snapshot of it's working.
new.jpg

Hi Rahul
If you do chose to fix people's homework before them then please take the time to explain what you have changed and why you changed it as you did. Otherwise how will they learn?

not to mention: give them a chance to solve it themselves. his/her teacher won't be impressed by improved copy-paste skills

Member Avatar for Rahul47

If you do chose to fix people's homework before them.
not to mention: give them a chance to solve it themselves.

Thank You for reminding it. I appreciate it. I assumed that the user provided his work and I just improved upon it.

please take the time to explain what you have changed and why you changed it as you did.

There were not much changes in it. And I did added comments in the code itself for user.

I will still point out the minor changes I made.

my point is, it is better to give the comments, and let the OP make those changes himself. It'll force him to re-check and re-think his code.

Member Avatar for Rahul47

my point is, it is better to give the comments,

I will keep that in mind from next time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.