Not sure why this isn't working. I am having the main method call the sum functions with the parameters (2,2) but it is saying the 'void' type is not allowed here. I don't even know what that means as the 'void' java keyword isn't something we have gone over.
Thanks for any help!
Love the site!
Dan
package com.codegym.task.task01.task0128;
/*
As simple as 2+2
*/
public class Solution {
public static void main(String[] args) {
System.out.println(sum(2,2));
}
public static void sum(int a, int b) {
int c = a + b;
System.out.print(c);
}
}
Void type not allowed here?
Under discussion
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
druck
16 June 2019, 08:28
you can also remove System.out.println in main method and just write sum........
apart from lftitime Alin
0
Iftime Alin
8 June 2019, 14:47
public static int sum(int a, int b) {
int c = a + b;
return c;
}
This above is your method and below is your main.
public static void main(String[] args) {
System.out.println(sum(2,2));
}
0
yz Backend Developer
4 June 2019, 06:03
just dlt void and write int
+1
Iftime Alin
4 June 2019, 04:49
Hello
void means you dont return a type of data, here you have sum of two integers, so in stead of void it is int and in stead of System.out.print(c); you will use return c; because you will print the result in main, the method is only for calculation.
Hope this will help.
0
Dan
5 June 2019, 20:01
Thanks. I tried that...
public class Solution {
public static int main(String[] args) {
return c;
}
and it said:
cannot find symbol symbol: variable c location: class com.codegym.task.task01.task0128.Solution.
But when I thrown in "int c;" above it,...
public class Solution {
public static int main(String[] args) {
int c;
return c;
}
it gives me:
variable c might not have been initialized.
0
joseph noelfils
6 June 2019, 15:09
to initialize a variable you need to attach a value to it
for example
int x = 8 is an initialized variable while
int x isnt
0
Dan
6 June 2019, 19:42
Thanks, but why would we assign a value to "c" to initialize it when "c" is what is calculated.
Thanks all for your help.
0
Dan
6 June 2019, 19:44
disregard, just answered my question
0