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;
        }


}

Dani AI

Generated

Most likely causes for "it compiles but will not run" here are either no command-line argument was supplied or an infinite loop inside the reversing routine. already hinted at the first: main only iterates over args, so if args.length == 0 nothing executes and nothing prints. If an argument was supplied but the program appears to hang, the problem is the range-check that surrounds the digit-extraction inside the while (number != 0) loop — for valid three-digit inputs that guard is false, the loop body never executes, number never changes and the loop never ends.

Concrete fixes:

  • Move input validation out of the digit-extraction loop. Check that the parsed integer is in 100..999 before calling the reverse routine. Inside the reversing loop do the digit extraction unconditionally so number decreases to 0.
  • Correct the off-by-one range: a three-digit number is 100 through 999 (not 99 or 1000).
  • Fix the checkDigits logic: compute the reverse once, then if reverse >= number signal an error or return false. Do not throw new Error for argument validation; use IllegalArgumentException or return a boolean and handle it in main.
  • Avoid recomputing the reverse multiple times: capture the returned reverse in main and pass it into subtract/add, or compute it once and reuse it.
  • Wrap Integer.parseInt in try/catch to handle non-numeric input and optionally print a short usage message. To verify argument passing, a quick debug print of args.length helps, and an example run is java Rev 123.

provided a working rework that follows these ideas; as suggested, include short comments explaining each change so the original author () learns the rationale rather than just accepting a replacement.

Recommended Answers

All 7 Replies

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 Member #1042208

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 Member #1042208

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 Member #1042208

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.