return statement

can one have 2 return statements in a method : e.gIf (i = 0)return 1;elsereturn 0;
[117 byte] By [nutojavaa] at [2007-10-2 4:37:43]
# 1
> can one have 2 return statements in a method : > e.gIf (i = 0)>return 1;>else>return 0;Yep. Be careful about the difference between = and ==, though.
yawmarka at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 2

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;

}

jesperdja at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

happy_hippoa at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 4

> 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.

yawmarka at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 5
UJ should arrive shortly, to extol the virtues of structured programming...
yawmarka at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 6

> 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

kilyasa at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 7

> // 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. :-)

happy_hippoa at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 8

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;

}

}

nutojavaa at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 9
Could you post your code again with the code formatting tags? http://forum.java.sun.com/help.jspa?sec=formatting
aniseeda at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...
# 10
When you post code, please post it between [code] and [/code] tags (you can use the "code" button on the message posting screen). It makes your code much easier to read and prevents accidental markup from the forum software.
yawmarka at 2007-7-16 0:10:42 > top of Java-index,Java Essentials,New To Java...