Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: calling a method into System.out.print() method????? solved

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default calling a method into System.out.print() method????? solved

    is it possible to call a method into another method?


    i can see the error into my code and it's not possible for me to call range() into System.out.print() function.


    if possible give me a solution here.



    public class Main {

    public static void main(String[] args) {

    A obj1=new A();
    System.out.println("The value of x from the class A is "+obj1.x);
    System.out.println("The value of y from the class A is "+obj1.y);
    obj1.range();
    System.out.print(obj1.range()); // error shows here. it's not executed . method calling method.
    }
    }

    class A{

    int x=2,y=7;
    public void range()
    {
    int z=x*y;

    }
    }
    Last edited by sumon_bd; December 13th, 2021 at 05:28 AM. Reason: solved

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: calling a method into System.out.print() method?????

    Using a method call as part of a String requires that the method return a value.
    range is void so the compiler can not get any value to convert and add to the String.

    When I compile your code I get this error message:
    error: 'void' type not allowed here
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sumon_bd (December 4th, 2021)

  4. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: calling a method into System.out.print() method?????

    Quote Originally Posted by Norm View Post
    Using a method call as part of a String requires that the method return a value.
    range is void so the compiler can not get any value to convert and add to the String.

    When I compile your code I get this error message:

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    yes i got the solution from suggestion of intelji.. the method needs a return value.

    thank you.

Similar Threads

  1. Difficulty calling method in another method
    By Yearplanner in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2019, 02:13 PM
  2. Method not calling?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 20th, 2013, 12:04 PM
  3. calling this method
    By antnas in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:32 PM
  4. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 01:32 PM
  5. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 AM