void method?  
Author Message
Irlan agous





PostPosted: 2004-12-16 5:15:00 Top

java-programmer, void method? Hello,
I am not sure were to put the return, i want to return the sum of the 3
digits, but it gives me an error becouse, it expect a void method

Irlan


public static void main(String[] args) {

//declareer getal
long getal = 456;

//invoke method
Sumdigits(getal);
}
//method declaration
public static int Sumdigits(long n){

int num2 = (int)n % 10;
int num1 = (int)n / 10;
int num0 = (int)num1 % 10;
int num100 = (int)n / 100;
return
System.out.println("de sum is" +num2+ "+" + num0 + "+" +num100);

}
}


 
klynn47





PostPosted: 2004-12-16 5:19:00 Top

java-programmer >> void method? I would suggest you return the sum,
return(num2 + num0 + num100);

Then in your main method, print the result of calling the method
System.out.println(Sumdigits(getAl));

 
Starshine Moonbeam





PostPosted: 2004-12-16 8:39:00 Top

java-programmer >> void method? In article <c9958$41c0a951$513b6900$email***@***.com>, Irlan agous
(email***@***.com) dropped a +5 bundle of words...

> Hello,
> I am not sure were to put the return, i want to return the sum of the 3
> digits, but it gives me an error becouse, it expect a void method
>
> Irlan
>
>
> public static void main(String[] args) {
>
> //declareer getal
> long getal = 456;
>
> //invoke method
> Sumdigits(getal);
> }
> //method declaration
> public static int Sumdigits(long n){
>
> int num2 = (int)n % 10;
> int num1 = (int)n / 10;
> int num0 = (int)num1 % 10;
> int num100 = (int)n / 100;
> return
> System.out.println("de sum is" +num2+ "+" + num0 + "+" +num100);
>
> }
> }

lose the return statement and declare the return type to int.

public int sumDigits(long n) {

}


--
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM









 
 
Starshine Moonbeam





PostPosted: 2004-12-16 8:40:00 Top

java-programmer >> void method? In article <c9958$41c0a951$513b6900$email***@***.com>, Irlan agous
(email***@***.com) dropped a +5 bundle of words...

> Hello,
> I am not sure were to put the return, i want to return the sum of the 3
> digits, but it gives me an error becouse, it expect a void method
>
> Irlan
>
>
> public static void main(String[] args) {
>
> //declareer getal
> long getal = 456;
>
> //invoke method
> Sumdigits(getal);
> }
> //method declaration
> public static int Sumdigits(long n){
>
> int num2 = (int)n % 10;
> int num1 = (int)n / 10;
> int num0 = (int)num1 % 10;
> int num100 = (int)n / 100;
> return
> System.out.println("de sum is" +num2+ "+" + num0 + "+" +num100);
>
> }
> }

lose the return statement OR declare the return type to int.

public int sumDigits(long n) {

}

Sorry about that.


--
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM









 
 
Starshine Moonbeam





PostPosted: 2004-12-16 9:22:00 Top

java-programmer >> void method? In article <c9958$41c0a951$513b6900$email***@***.com>, Irlan agous
(email***@***.com) dropped a +5 bundle of words...

> Hello,
> I am not sure were to put the return, i want to return the sum of the 3
> digits, but it gives me an error becouse, it expect a void method
>
> Irlan
>
>
> public static void main(String[] args) {
>
> //declareer getal
> long getal = 456;
>
> //invoke method
> Sumdigits(getal);
> }
> //method declaration
> public static int Sumdigits(long n){
>
> int num2 = (int)n % 10;
> int num1 = (int)n / 10;
> int num0 = (int)num1 % 10;
> int num100 = (int)n / 100;
> return
> System.out.println("de sum is" +num2+ "+" + num0 + "+" +num100);
>
> }
> }
>

found void but expected int.

Which int are you returning? Pick one and then just

return num2; (for example)

Why are you trying to return something you're just printing to the
screen anyway? What I would do, is just lose the return statement and
just print the output to the screen and make the return type void. You
don't really need to use return unless you need the value to appear in
another method or something.

For instance

useNum(sumDigits());

Then you'd want a return statement because you're expecting the value of
sumDigits to feed the useNum method.


--
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM









 
 
Juha Laiho





PostPosted: 2004-12-17 1:45:00 Top

java-programmer >> void method? Starshine Moonbeam <email***@***.com> said:
>In article <c9958$41c0a951$513b6900$email***@***.com>, Irlan agous
>(email***@***.com) dropped a +5 bundle of words...
>> I am not sure were to put the return, i want to return the sum of the 3
>> digits, but it gives me an error becouse, it expect a void method

Well, you already got your answer - i.e. to return from Sumdigits
method the sum you want to return (instead of printing it), and
then print it in the main method.

>Why are you trying to return something you're just printing to the
>screen anyway?

This looks pretty much like a homework assignment, where the intention
is to have the student to learn the mechanism of returning values from
method calls.

>What I would do, is just lose the return statement and just print the
>output to the screen and make the return type void. You don't really
>need to use return unless you need the value to appear in another
>method or something.

Which makes the method completely dependent on the single purpose of
just printing the result. Making the method return whatever it was
supposed to process will leave it to the caller to decide how the
processed information (return value) is handled from that point on.

So, returning a value from a method rather than printing it out will
promote reusability.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
 
Starshine Moonbeam





PostPosted: 2004-12-17 2:49:00 Top

java-programmer >> void method? In article <cpshjn$2gl$email***@***.com>, Juha Laiho
(email***@***.com) dropped a +5 bundle of words...

> Starshine Moonbeam <email***@***.com> said:
> >In article <c9958$41c0a951$513b6900$email***@***.com>, Irlan agous
> >(email***@***.com) dropped a +5 bundle of words...
> >> I am not sure were to put the return, i want to return the sum of the 3
> >> digits, but it gives me an error becouse, it expect a void method
>
> Well, you already got your answer - i.e. to return from Sumdigits
> method the sum you want to return (instead of printing it), and
> then print it in the main method.
>
> >Why are you trying to return something you're just printing to the
> >screen anyway?
>
> This looks pretty much like a homework assignment, where the intention
> is to have the student to learn the mechanism of returning values from
> method calls.
>
> >What I would do, is just lose the return statement and just print the
> >output to the screen and make the return type void. You don't really
> >need to use return unless you need the value to appear in another
> >method or something.
>
> Which makes the method completely dependent on the single purpose of
> just printing the result. Making the method return whatever it was
> supposed to process will leave it to the caller to decide how the
> processed information (return value) is handled from that point on.
>
> So, returning a value from a method rather than printing it out will
> promote reusability.
>

Sure. And you could do both, with a good reason but for the example OP
gave, return wouldn't have done a whole lot, which is why I went with
the "What I would have done...."



--
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM