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);
    }
}
Karthik Suresh
  • 367
  • 7
  • 23
  • 5
    Java is pass by value, so how come you think you have a pass by reference issue in Java? – Stultuske May 27 '15 at 10:45
  • 1
    Java wont supports pass by reference – Sasikumar Murugesan May 27 '15 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](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1). – mhlz May 27 '15 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. – Stultuske May 27 '15 at 10:51
  • @Stultuske No need to add that little quip in parentheses there. – mhlz May 27 '15 at 10:53
  • @Stultuske I meant pass by value just got it typed wrong to refrence – Darshan Lila May 27 '15 at 10:55
  • 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. – RealSkeptic May 27 '15 at 11:11

2 Answers2

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.

Alok Mishra
  • 926
  • 13
  • 39
  • 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. – RealSkeptic May 27 '15 at 11:03
  • 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 – Alok Mishra May 27 '15 at 11:11
  • @AlokMishra for me the the actual problem is in the arguments which am passing from smaller() for compute(); – Karthik Suresh May 27 '15 at 13:15
  • yes may be . Another thing is that you have checked `if (ee.length() >= k)` in `compute()` but value of `k` never gets modified .@KarthikSuresh – Alok Mishra May 27 '15 at 13:21
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());
    }
}
Rajesh
  • 2,135
  • 1
  • 12
  • 14