Putting a return value into System.Out.Println?

Hi

I have one piece of code that runs using 'return' in the method signature.

I have to test this method from another object but have to return the value using System.out.prinln. I've tried calling the method and the variable in the original class from the test class but keep getting the value 0 in the system output terminal

For a further explanation -

The original class just returns the value in a window as 'return' is supposed to but I cant get it to put this value to the system.out.println in the terminal. (Instead getting 0)

I have made a reference to the original and done everything 'right' as the other method calls work, and honestly, this one does too, but i just need it to print the output to the terminal ONLY when being tested.

It goes something like this

publicvoid testCallBehaviour(int duration){

phone.MakeCall(duration);

System.out.println(phone.Duration);

}

Where Duration is the original and duration is the duration for the test class.

I've tried variations but none seem to return the correct value...

[1270 byte] By [Sacreda] at [2007-10-2 4:37:04]
# 1
Seeing the definition of MakeCall would probably help. :^)Also, you should start you method names in lower case, just as a convention. Class names start out capitalized. And constants are all-caps.- Saish
Saisha at 2007-7-16 0:09:44 > top of Java-index,Java Essentials,New To Java...
# 2

I seem to have done the test procedure right but now im having trouble making return 'return' nothing.

I made a return method with an IF statement and several Else If statements, and after these have been closed using {} I get the compile error 'missing return statement'

Is there a way to write this out? Or can I edit the below code to sort this out?

public int MakeCall (int Duration)

{

NewDuration = Duration;

if(CreditAvailable < 1.0 || DisplayNumber.equals("") || Duration < 1)

{

System.out.println("-1");

}

else if(Duration > 1 && Duration <= hRateDuration && CreditAvailable > Duration * hRate && CreditAvailable > 1) //WORKING AS OF 23.10

{

CreditUsed = hRate * Duration;

CreditAvailable = CreditAvailable - CreditUsed;

LastNumber = DisplayNumber;

return Duration;

//System.out.println(Duration);

}

else if(Duration > 1 && Duration >= hRateDuration && CreditAvailable > lRate * (Duration - hRateDuration) + (hRateDuration * hRate)) //WORKING AS OF 24.10!!!!

{

CreditUsed = lRate * (Duration - hRateDuration) + (hRate * hRateDuration);

CreditAvailable = CreditAvailable - CreditUsed;

LastNumber = DisplayNumber;

return Duration;

//System.out.println(Duration);

}

else if(Duration > 1 && Duration <= hRateDuration && CreditAvailable < (Duration * hRate)) // WORKING AS OF 24.10

{

NewDuration = (double)Duration;

CallDiff = (Duration * hRate) - CreditAvailable;

NewDuration = Duration - (CallDiff/hRate);

Duration = (int)NewDuration;

CreditUsed = hRate * Duration;

CreditAvailable = CreditAvailable - CreditUsed;

LastNumber = DisplayNumber;

return Duration;

//System.out.println(Duration);

}

else if(Duration > 1 && Duration >= hRateDuration && CreditAvailable < (lRate * (Duration - hRateDuration) + (hRateDuration * hRate))) //WORKING AS OF 24.10 @ 16:02!!!!

{

NewDuration = (double)Duration;

NewDuration = (CreditAvailable - hRate*hRateDuration)/lRate + hRateDuration;

Duration = (int)NewDuration;

CreditUsed = (lRate * (Duration - hRateDuration)) + (hRate * hRateDuration);

CreditAvailable = CreditAvailable - CreditUsed;

LastNumber = DisplayNumber;

return Duration;

//System.out.println(Duration);

}

return ;

}

Sacreda at 2007-7-16 0:09:44 > top of Java-index,Java Essentials,New To Java...
# 3

You have two options:

> Return Integer instead of int. Integer can be null. So in your last return, you would return null and check on the calling end whether null was received.

> Leave it as an int, but return a 'sentinel' value instead. A sentinel is a value you do not expect the method to regularly return. For example, if your method will return a count, one would expect values zero or greater. Therefore, you could return a sentinel value of -1 indicating an error or nothing was found.

- Saish

Saisha at 2007-7-16 0:09:44 > top of Java-index,Java Essentials,New To Java...
# 4
is there no way to make it so that the last return simply doesnt do anything? As it is disrupting my test results, which need to come out as expected, as each of them have, but should return no extra values
Sacreda at 2007-7-16 0:09:44 > top of Java-index,Java Essentials,New To Java...
# 5

First, understand that if a method declares a return type, then according to the JLS, all non-exceptional returns must return a value of that type (or null if an object type). So, you CANNOT get around this. If you feel you need to get around this, you have designed your method incorrectly. Plain and simple.

Second, re-read the last reply I posted. Simply return -1, and check for a negative value on the other end. Think of it from the compiler's point of view, how should it handle a method that *might* return a value? It's up to you to deal with sentinel (-1 for an int) or null values.

- Saish

Saisha at 2007-7-16 0:09:44 > top of Java-index,Java Essentials,New To Java...