Page 1 of 1
how can i print
Posted: Sun Feb 11, 2018 12:29 am
by maty.tsoraro@gmail.com
hi,
hope i am not spamming here to much

the code is compiling but i cannot print it
doesnt allow me me to use print in void
this is the method :
public class Exam{
public void f(int n){
int j = 1 ;
while(j <= n)
j *= 2 ;
}
}
this is the main class :
public class main{
public static void main(String[] args){
Exam obj = new Exam();
obj.f(8);
}
}
Re: how can i print
Posted: Sun Feb 11, 2018 6:20 am
by maty.tsoraro@gmail.com
Hi!
Thanks a lot for posting, it helps to build the community
About your problem. It's difficult to know which program are you trying to execute.
To allow anybody to see your program you can paste the code (surrounded with the code tag, use the button </> so it's pretty formatted) and also:
1) Right Click on your project, select Edit Project Properties and uncheck the private checkbox (this is to allow anybody to see your program) and click "Save"
2) Right click on your project and click "Get Url", this will give you the URL to share your program
3) Paste the URL in the post
If somebody then use the URL, they will get a clone of your project and can help you faster
Thanks!
David
Re: how can i print
Posted: Mon Feb 12, 2018 4:29 am
by maty.tsoraro@gmail.com
Re: how can i print
Posted: Mon Feb 12, 2018 11:26 am
by dbremmen@gmail.com
Hi, I think your code should be like this:
I changed:
* the method signature from void to int
* added the return statement to the end of the method
* added the System.out,println to print the result of the method
Code: Select all
public class main{
public static void main(String[] args){
Exam obj = new Exam();
System.out.println(obj.f(8));
}
}
public class Exam {
public int f(int n) {
int j = 1 ;
while(j <= n) {
j *= 2 ;
}
return j;
}
}
Re: how can i print
Posted: Mon Feb 12, 2018 4:28 pm
by maty.tsoraro@gmail.com
first of all thanks alot
but'what if i want the method to be with void
if so is ther a way to print the result ?
Re: how can i print
Posted: Mon Feb 12, 2018 7:10 pm
by dbremmen@gmail.com
You're welcome!
If you want your method to be void, you can print the result inside the void method like this:
Code: Select all
public class Exam {
public void f(int n) {
int j = 1 ;
while(j <= n) {
j *= 2 ;
}
System.out.println(j);
}
}
"void" means that the method does not return the value, that's why you can't print the result of the void method when you call it
Re: how can i print
Posted: Mon Feb 12, 2018 7:24 pm
by maty.tsoraro@gmail.com
oh ok
great
now i understand !!
