Yes, you can, but some people regard this as bad practice - it can make it more difficult to see what the flow of control is, especially if the method is longer. If you have only one return statement, it's often easier to see what happens in the method.
public int myMethod(int i) {
int result;
if (i == 0) {
result = 1;
}
else {
result = 0;
}
return result;
}
> Yes, you can, but some people regard this as bad
> practice - it can make it more difficult to see what
> the flow of control is, especially if the method is
> longer.
On the other hand, if the method is that long, you should consider refactor it into smaller pieces.
> If you have only one return statement, it's
> often easier to see what happens in the method.
// easier to read, yet, IMO...
public int myMethod(int i) {
return (i == 0) ? 1 : 0;
}
> some people regard this as bad practice.
I'm not one of those people. IMHO, Java isn't C. One should return from a method when you have the return value. I agree with happy_hippo on the general rule that if a method is so long that it's confusing to track the flow, refactor it.
> Yes, you can, but some people regard this as bad
> practice - it can make it more difficult to see what
> the flow of control is, especially if the method is
> longer. If you have only one return statement, it's
> often easier to see what happens in the method.
your comments just made me think and I referred to "refactoring" by Martin Fowler. Didnt find anything against multiple return statements, as a matter of fact I can see him doing that at a couple of places
> // easier to read, yet, IMO...
> public int myMethod(int i) {
>return (i == 0) ? 1 : 0;
> }
>
That would be my personal preference too.
Of course, one can get away with the conditional altogether. Just create a map that maps every possible int-value to a return value. :-)
Thank you for that, i actually figured it out after a while, probably a mental block.I have written 2 programs tonight both dealing with user defined methods. I know my logic is probably not up to scratch but the following is my attempt to get it right.
I have only started with java, so i dont know much about try and catch statements.Please assist me with logic and the java.lang.NoSuchMethodError that is being thrown at run time.
import javax.swing.*;
import java.awt.Container;
public class DuplicateArray extends JApplet
{
public void init()
{
Container c = getContentPane();
JTextArea outputArea = new JTextArea(11, 10);
c.add(outputArea);
int b[] = new int[5];
int j;
String output = "Array before loop\n";
for(int i = 0; i <= 4; i++)
output += i + " " + b + "\n";
String numStr = JOptionPane.showInputDialog("Enter first number");
int number = Integer.parseInt(numStr);
searchArray(b, number);
for(int i = 0; i <= 4; i++) // After we have determined whether the number alreay exists
{ // we use the for loop to enter the approprate values
if (number != 0)
{
b = number;
output += i + " " + b + "\n";
}
else
{
output += "\n";
numStr = JOptionPane.showInputDialog("Enter first number");
number = Integer.parseInt(numStr);
searchArray(b, number);
}
numStr = JOptionPane.showInputDialog("Enter first number");
number = Integer.parseInt(numStr);
searchArray(b, number);
}
outputArea.setText(output);
//JOptionPane.showMessageDialog(null, outputArea, "Duplicate Array", JOptionPane.INFORMATION_MESSAGE)
}
public int searchArray(int c[], int num) // this method determines if the number already exists in the array
{int a = 0;
for(int i = 0; i < c.length; i++)
{
if (num == c)
a = num;
else
a = 0;
}
return a;
}
}