why error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

why error?

i think i lack the display? the System.out.print function display the result. but what symbols should i print? too dizzy https://code.sololearn.com/ctay6MOKDmnW/?ref=app

25th Feb 2022, 6:35 AM
Japheth
15 Answers
+ 4
Yes, code is okay. You read the number n in main, give that to the sum function and print the value returned: // in main after scanner close System.out.print(sum(n));
25th Feb 2022, 7:37 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Japheth Just print sum after taking input System.out.print(sum(n));
25th Feb 2022, 7:36 AM
A͢J
A͢J - avatar
+ 2
A͢J Ani Jona 🕊 huhuhu thank u both of you i actually consumed a really big amount of time researching here and there. thank you so much will take note of that
25th Feb 2022, 7:41 AM
Japheth
+ 2
Japheth Method is used to perform a particular task so here sum method is performing a task to get total of all numbers from 1 to n So here sum is a method which is accepting one parameter which is your input. As sum method is returning total of 1 to n so we have to print that returned value. You can either print by just calling method or you can store returned value to a new variable and then you can print that variable like this: int sum = sum(n); System.out.print(sum);
25th Feb 2022, 7:57 AM
A͢J
A͢J - avatar
+ 1
10904647. Means. Hougp
26th Feb 2022, 11:04 AM
Justin Eze
0
you didn't use sum method to get the result to print it
25th Feb 2022, 7:05 AM
Mohamed
Mohamed - avatar
0
M O H A M E D im sorry how exactly? is the code okay?is the display is the only thing im lacking of? System.out.print(sum); like how
25th Feb 2022, 7:15 AM
Japheth
0
you need tp pass the variable to sum method
25th Feb 2022, 7:21 AM
Mohamed
Mohamed - avatar
0
sum take one int parameter
25th Feb 2022, 7:22 AM
Mohamed
Mohamed - avatar
0
should i print the result in main method?
25th Feb 2022, 7:33 AM
Japheth
0
so (sum(n)) is basically giving the n(which is the input) to the sum method? like distributing the n to the sum method?
25th Feb 2022, 7:48 AM
Japheth
0
You have just to write System.out.println(sum(n));
25th Feb 2022, 10:57 PM
Gnandal Élisée
Gnandal Élisée - avatar
0
You haven't called the sum function with the input as a parameter. A great example as already mentioned quite a few times is: System.out.print(sum(n)). That way you're calling the sum function with your input as a parameter (in this case n). It does the calculations inside the sum function and prints the result. Hope that helped!
26th Feb 2022, 12:29 PM
George Bilias
George Bilias - avatar
0
Manav Roy the close function is to close the Scanner. If you don't close the Scanner then java won't garbage collect the Scanner and it might lead to memory leaks.
26th Feb 2022, 1:31 PM
George Bilias
George Bilias - avatar
0
Blessings ere beneath cthullic embryoderyies
26th Feb 2022, 8:04 PM
Michael Archer
Michael Archer - avatar