2

During the method call compute(ee, temp1, s1, k) from method smaller() the argument value which am passing is not same as the one which am receiving, can someone help me with this issue?

*This is my problem statement-Get the least number after deleting k digits from the input number. For example, if the input number is 24635,the least number is 23 after deleting 3 digits.

I am expecting the final output to be 23 but getting infinite loop.

public class Delete3 {


    public static void main(String[] args) {
        int k = 3;
        int num = 24635;
        StringBuffer num1 =new StringBuffer(Integer.toString(num));
        int temp = 0;
        StringBuffer s = new StringBuffer(num1);
        temp = Integer.parseInt(s.deleteCharAt(0).toString());
        temp = compute(num1, temp, s, k);
        System.out.println(temp);
    }

    static int compute(StringBuffer num2, int temp, StringBuffer s, int k) {
        while (Integer.toString(temp).length() >= k) {
            for (int i = 0; i < num2.length(); i++) {
                StringBuffer s1 = new StringBuffer(num2);
                String a = s1.deleteCharAt(i).toString();
                int temp1 = Integer.parseInt(a);
                if (temp > temp1) {
                    temp = Integer.parseInt(a);
                }
            }
            StringBuffer ee = new StringBuffer(Integer.toString(temp));
            if (ee.length() >= k) {
                smaller(temp, k);
            }
        }
        return temp;
    }

    static void smaller(int temp, int k) {
        StringBuffer ee = new StringBuffer(Integer.toString(temp));
        StringBuffer s1 = new StringBuffer(ee);
        StringBuffer s2 = new StringBuffer(ee);
        Integer temp1 = Integer.parseInt(s2.deleteCharAt(0).toString());
        compute(ee, temp1, s1, k);
    }
}
7
  • 5
    Java is pass by value, so how come you think you have a pass by reference issue in Java? Commented May 27, 2015 at 10:45
  • 1
    Java wont supports pass by reference Commented May 27, 2015 at 10:49
  • 1
    Can you please add some comments to that code? Right now it's really hard to understand what you're trying to accomplish and how it goes wrong. In general though, Java can't pass references ever. It's always pass by value. Commented May 27, 2015 at 10:51
  • 1
    @DarshanLila "Everything in java is pass by reference" is nonsense. According to the Java Specs (I assume they are written by those who know what they are talking about) Java is pass by value. There is no such concept as 'Pass by reference' in Java. Commented May 27, 2015 at 10:51
  • 1
    I think you should revise your program. It does a lot of things that are not needed and make it harder to read and understand. For example, the s parameter to compute is never used. And k is the number of digits to remove - not the number that is supposed to remain. Give your parameters meaningful names, and try to follow the logic and re-think it. Commented May 27, 2015 at 11:11

2 Answers 2

1

The infinite loop you are getting is because you are calling smaller() from inside compute() and compute() from inside smaller(). If this is done intentionally then also add a terminating condition , which would prevent it from looping infinitely.

Sign up to request clarification or add additional context in comments.

4 Comments

It's supposed to stop when the length of the new number is smaller than k. So there is a stop condition - the question is why it doesn't reach it.
yes i agree ,as the OP hadn't mentioned the problem statement in the first place , so what i could notice just by looking at the code was that those methods were calling each other and when i ran the program in my IDE , i found that those methods keep calling each other .@RealSkeptic
@AlokMishra for me the the actual problem is in the arguments which am passing from smaller() for compute();
yes may be . Another thing is that you have checked if (ee.length() >= k) in compute() but value of k never gets modified .@KarthikSuresh
0

You can use simple code as below :

import java.util.Arrays;

public class Delete3 {

    public static void main(String[] args) {

        int num = 246235789;
        int numDigitRequired = 2;

        System.out.println(getLeastNum(num, numDigitRequired));
    }

    static int getLeastNum(int num, int numDigitRequired) {

        char[] a = (num + "").toCharArray();

        Arrays.sort(a);

        StringBuffer s = new StringBuffer();

        for (int i = 0; i < numDigitRequired; i++)
            s.append(Character.getNumericValue(a[i]));

        return Integer.parseInt(s.toString());
    }
}

7 Comments

it will fail for 24690
in case of 24690 , what output do you expect ? 20 or 02 ? @KarthikSuresh
@AlokMishra it should be 02
what difference does it make ? 02 and 2 are equal . we can not fool JVM.@KarthikSuresh
@AlokMishra my bad it should be 20
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.