how can i print

Do you have a compilation error or a difficult problem to solve? Ask for help here
Post Reply
maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

how can i print

Post by maty.tsoraro@gmail.com » Sun Feb 11, 2018 12:29 am

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);
}
}

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how can i print

Post by maty.tsoraro@gmail.com » Sun Feb 11, 2018 6:20 am

Hi!
Thanks a lot for posting, it helps to build the community :D

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

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how can i print

Post by maty.tsoraro@gmail.com » Mon Feb 12, 2018 4:29 am


dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: how can i print

Post by dbremmen@gmail.com » Mon Feb 12, 2018 11:26 am

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;
}
}



maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how can i print

Post by maty.tsoraro@gmail.com » Mon Feb 12, 2018 4:28 pm

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 ?

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: how can i print

Post by dbremmen@gmail.com » Mon Feb 12, 2018 7:10 pm

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

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how can i print

Post by maty.tsoraro@gmail.com » Mon Feb 12, 2018 7:24 pm

oh ok
great
now i understand !! :)

Post Reply