#How did I make an infinite loop

16 messages · Page 1 of 1 (latest)

steady plinth
#
public class EvenAgainstOdd {

    // Write your method here

    public static long setEvenAgainstOdd(long num) {
    long output = 0;
    int power = 0;
    while (num > 0) {
        num++;
        if (num % 2 == 0) {
            output = num * ((long) Math.pow(10, power) + output);
        power++;
        }

    }
        return output;
    }


    // Additional test in main
    public static void main(String[] args) {

        System.out.println(setEvenAgainstOdd(2450667619L)); // 5719240666

    }

}
tribal joltBOT
#

Hey, @steady plinth!
Please remember to /close this post once your question has been answered!

steady plinth
#

My prompt: For this challenge, you need to create a public static setEvenAgainstOdd method, which takes a long input (i.e., a possibly very large integer) and returns another long value.

What the method actually does is a little funky: It parses through the digits of its argument and pushes the odd values to the left and, consequently, pushes the even values to the right. The order of the odd and even digits are respected. A couple examples are worth a thousand words.

Input:   123405
Output: 135240

Input:   3045211
Output: 3511042

Only one simple rule: If the method's argument is negative, the sign is switched in the output.

Input:   -452
Output:  542
manic narwhal
#

Num is always going to be > 0 so the while loop is infinite

uncut mason
manic narwhal
#

Well, true. But I guess I meant for all intents and purposes

uncut mason
#

I don't see you passing through the digits of anything

#

@steady plinth

#

so Long.MAX_VALUE is 9223372036854775807... which should result in 9337357757222068480, but that's too big for a long

#

So is that supposed to throw an exception?

#

Are you allowed to use collections? streams?

steady plinth
#

sorry I was at work

#

I see whats wrong

#

thank you all