Java programming Reverse Integers by calling a method?
-
Hello, I am new to java programming and I apologize if the way I am asking for help is a little hard to understand because I don't fully comprehend the terminology of java. Essentially, I am asked in a homework assignment to use a while loop to present the reverse integers of 12345 in a pop up box window format. Using a return statement and by calling a method at the end of the main method. I suppose that's a clear as I can make it as far as pseudo code goes, here is what I have right now-- maybe this will make it easier for you to understand what I'm trying to do. The code doesn't compile and I'm clearly missing something. //Instructor: Jigang Liu //Student: Ryan Carlson //assignemtn: 5 part 2 //Compiler: JDK 1.7 with Text Pad //Due Date: 02/27/12 //Description: Based on the exercise in the book 5.4 on page 189, us the template we discussed in class in order to call for your method to reverse the input import javax.swing.JOptionPane; public class MyReverse { public static void main(String[] args) { //define variables String num1Str, message; int num1, reverseNum; //Enter an integer num1Str = JOptionPane.showInputDialog("Enter a positive integer:"); num1 = Integer.parseInt(num1Str); //Call a method "reverseIntegers with an argument "num1" reverseNum = reverseIntegers(num1); //Assemble the output message = "Reverse integer for " + num1 + " is " + reverseNum; //output the result JOptionPane.showMessageDialog(null, message); }//end of the main public static int reverseIntegers(int myInput) { int temp1 = myInput % 10; while(myInput > 0) { temp1 = myInput % 10; //get the last digit myInput = myInput / 10;//remove the last digit return temp1; } Let me know if you can help, thanks.
-
Answer:
Okay your biggest mistake and also the reason why it won't compile is the return statement inside the loop, because it could be possible that a loop doesn't run and in this case would be no return statement! Another problem with the return statement inside a loop, is that the return statement exits the loop immediately -> therefore ther would only be one run inside the loop (which makes no sense). Just write the return statement outside the loop. Your second mistake is that you reset the temp1 mehod everytime: temp1 = myInput % 10; //you'll only return one digit!! To add digits you can write: temp1 = temp1 + myInput % 10; or temp1 += myInput % 10; However that's still not the whole solution. Let's solve it by using an example, our numer is 28. We want to get 82, but if we add 8 & 2 like in the code below - it won't work. temp1 = temp1 + myInput % 10; What we have to do is always multiplying our temp1 by 10 before adding the next number to move our current numbers one position further. 1) temp1=8 2) temp1=temp1*10 -> temp1=80 3) temp1=temp1+2 -> temp1=82 So your final method would look like this: public static int reverseIntegers(int myInput) { int temp1 = myInput % 10; while(myInput > 0) { temp1 = temp1 * 10; //Move our numbers one position to the left leaving a zero on the right temp1 = temp1 + myInput % 10; //Fill in the real number instead of the zero myInput = myInput / 10;//Remove the last digit and repeat } return temp1; } Feel free to ask if you don't understand. Cheers Raphael
Ryan at Yahoo! Answers Visit the source
Related Q & A:
- How to invoke a method at particular time asynchronously every day in Mac application?Best solution by Ask Different
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How to call a method once everyday?Best solution by Stack Overflow
- Computer Science Java Programming?Best solution by AllExperts
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.