-1

the directions are the following: input an integer, then double integer value, then call your static void reverse( long i ) method. The method void reverse( long i) displays the integer in reverse terminated by a new line.

I need help in reversing the integer. I think I have the rest of the code correctly. But, correct me if I'm wrong.

    import java.util.Scanner;

public class Exercise5_3M {
    public static void reverse(String[] args) {
    Scanner input = new Scanner(System.in);

    //input an integer
    int num= input.nextInt();
    int result=0;

     //Double the intger
    while(num>0){
    result= num *2;

        }


System.out.print(result);
    }
}
0

2 Answers 2

0

at first of all you need void reverse( long i) instead void reverse(String[] args) at second

while(num>0){
    result= num *2;
}

never end, because num never change

3
  • And if you think there is no reason to have a while. It would just to double up. Oct 12, 2011 at 8:01
  • mhm, that's weird. I had tried that before- inputting reverse (long i), and it gave me a compile error. Maybe I changed something and didn't realize it. THanks! Gonna try to figure out the second part now. Aren't I saying in my code, that the number is greater than zero and the result will equal the number multiplied by two. How does it never end?
    – user972077
    Oct 12, 2011 at 8:18
  • @user972077 The while loop body executes until the condition is no longer true (or, to put it another way, it executes while the condition is true - hence a while loop). If the value of num never changes, that condition is always going to be true for any input greater than 0, so the body will simply be executed over and over again. It's what's called an infinite loop. Oct 12, 2011 at 8:55
0

Your method should be called "main" when you want to execute it on start. Second you created an infinity loop with your while when sb enters a positive number. A negative always returns 0. Third you have no method called reverse(long i). Fourth boundary check. Are only positive numbers allowed or negative also?

For a tip: You can reverse it in multiple ways. Convert your number to a string or char array and reverse it. Or you want to have a look at the mod operator "%".

1
  • I understand the concepts, but when I try to write it out, I constantly get a compile error. I understand java- conceptually- but, it's so hard to actually put it in code!
    – user972077
    Oct 12, 2011 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.