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 11 of 11

Thread: Printing the method return from another class

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Printing the method return from another class

    Hello, i cant get the return value to my Printer class from the Calculator class method. i have just started to learn about objects/constructors and in some way i cant understand it, even though i read and watch alot of tuts about it..guess something is just wrong called/declared here
    System.out.println("Sum of the numbers: " + Calculator.sum(sum));

    please help me thanks


    import java.util.Scanner;
     
    public class ObjectFunctionality {
        public static void main(String args[]) {
            Printer thing = new Printer();
            thing.Print();
        }
    }
     
     
    class Printer {
     
    	static int Print() {
     
    	Scanner reader = new Scanner(System.in);
     
    	System.out.println("Type in the first integer: ");
    	int num1 = reader.nextInt();
    	System.out.println("Type in the second interge: ");
    	int num2 = reader.nextInt();
     
    	Calculator.Sum(num1, num2);
     
    	System.out.println("Sum of the numbers: " + Calculator.sum(sum));
    	}	
    }
     
    class Calculator {
        static int Sum(int first, int second) {
            int sum = first + second;
            return sum;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing the method return from another class

    i cant get the return value to my Printer class from the Calculator class method.
    Please explain. What happens? If there are error messages, copy the full text and paste it here.
    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:

    xways (January 27th, 2021)

  4. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Printing the method return from another class

    Hey thanks for reply.
    Just to let u know, i tried alot of diffrent things in this line of code too, but this is the last i left in this line, anyway the error msg is:
    ObjectFunctionality.java:24: error: cannot find symbol
    System.out.println("Sum of the numbers: " + Calculator.sum(sum));
    ---------------------------------------------------------------------^

    cmd is pointing with ^ to the (sum)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing the method return from another class

    [QUOTE]ObjectFunctionality.java:24: error: cannot find symbol[/QUOTE
    The compiler can not find a declaration for the variable: sum that is in scope (declared within the same {}s) on line 24.
    Where is sum declared? Its declaration needs to be in scope at line 24.

    There is a variable: sum declared in the Calculator class but that is not in scope because it is in a different class.

    Note: Java naming conventions state that method names should start with lowercase letters. Sum and Print should be sum and print.
    Also method names should be verbs that describe what the method does.
    If you don't understand my answer, don't ignore it, ask a question.

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

    xways (January 27th, 2021)

  7. #5
    Junior Member
    Join Date
    Jan 2021
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Printing the method return from another class

    yes i understood, it basically cannot find sum in the Printer class. So thats the question im asking for, how to get that sum value from the Calculator class into the Printer class to print its value on screen?

    also about namin convention, the Calculator class was already written by the course im taking, its a 1:1 code copy, because they just want me to write the Printer class.
    so the only codeblock which is mine is the Printer Class. If you say so, i dont know why they have a uppercase there.
    i also tried already some things like in the Printer Classes print method

    int sumOfMethod = Calculator.Sum(sum);
    System.out.println("Sum of the numbers: " + sumOfMethod);

    but still same thing.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing the method return from another class

    how to get that sum value from the Calculator class
    The sum method returns the sum of the two int values passed to it.
    The calling code needs to save the value returned by the sum method in a variable so that it can work with it locally:
       int theSum = Calculator.sum(num1, num2);   // call sum method and save its results in theSum
    If you don't understand my answer, don't ignore it, ask a question.

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

    xways (January 27th, 2021)

  10. #7
    Junior Member
    Join Date
    Jan 2021
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Printing the method return from another class

    thanks again for the quick reply, yes i tried that already too but now i just copied yours in case my brain melted already these days im trying to figure that out and well it still doesnt work... :/

    ObjectFunctionality.java:25: error: missing return statement

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing the method return from another class

    class Printer {
    	static int Print() {
     
    	Scanner reader = new Scanner(System.in);
     
    	System.out.println("Type in the first integer: ");
    	int num1 = reader.nextInt();
    	System.out.println("Type in the second interge: ");
    	int num2 = reader.nextInt();
     
    	Calculator.sum(num1, num2);
     
    	int theSum = Calculator.sum(num1, num2);
    	System.out.println("Sum of the numbers: " + theSum);
    	}	
    }
    Why does the code call the sum method two times?

    The print method is declared to return an int value but there is no return statement that returns any value.
    What is the value the print method is supposed to return?
    Should the print method be declared as returning void and not int?
    If you don't understand my answer, don't ignore it, ask a question.

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

    xways (January 27th, 2021)

  13. #9
    Junior Member
    Join Date
    Jan 2021
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Printing the method return from another class

    the error is right after the System.out.println("Sum of the numbers: " + theSum); which is line24, and pointing to the next "}" bracket.(line25)

    import java.util.Scanner;
     
    public class ObjectFunctionality {
        public static void main(String args[]) {
            Printer thing = new Printer();
            thing.print();
        }
    }
     
     
    class Printer {
    	static int print() {
     
    	Scanner reader = new Scanner(System.in);
     
    	System.out.println("Type in the first integer: ");
    	int num1 = reader.nextInt();
    	System.out.println("Type in the second interge: ");
    	int num2 = reader.nextInt();
     
    	Calculator.sum(num1, num2);
     
    	int theSum = Calculator.sum(num1, num2);
    	System.out.println("Sum of the numbers: " + theSum);
    	}	
    }
     
    class Calculator {
        static int sum(int first, int second) {
            int sum = first + second;
            return sum;
        }
    }

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Printing the method return from another class

    Our posts crossed. Check my last post
    If you don't understand my answer, don't ignore it, ask a question.

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

    xways (January 27th, 2021)

  16. #11
    Junior Member
    Join Date
    Jan 2021
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Printing the method return from another class

    ye i forgot to put on // on that line Calculator.sum(num1, num2);, so the compiler ignores my statement and use yours. but oh my god.. changing static int print() { to static void print() { did it, now it works...
    since days i was caught up on other parts of the code and totally ignored this very line when i started to understand about objects chapter. thanks man for ruling that one out!! finally i can move on in the exercises xd

    btw: that was the example code after i put my code into, so i could see theirs:
    import java.util.Scanner;
     
    public class ObjectFunctionality {
        public static void main(String args[]) {
            Printer thing = new Printer();
            thing.Print();
        }
    }
    class Printer {
        int firstNumber, secondNumber, result;
        Scanner reader = new Scanner(System.in);
        Calculator counter = new Calculator();
     
     
        Printer() {
            System.out.print("Type in the first integer: ");
            firstNumber = reader.nextInt();
            System.out.print("Type in the second integer: ");
            secondNumber = reader.nextInt();
            result = counter.Sum(firstNumber, secondNumber);
        }
     
        public void Print() {      
            System.out.println("Sum of the numbers: " + result);
        }
    }
    class Calculator {
        static int Sum(int first, int second) {
            int sum = first + second;
            return sum;
        }
    }
    Last edited by xways; January 27th, 2021 at 04:29 PM.

Similar Threads

  1. return outside method
    By lucas29 in forum What's Wrong With My Code?
    Replies: 43
    Last Post: August 1st, 2014, 04:47 AM
  2. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  3. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  4. return from a method
    By MHS in forum Java Theory & Questions
    Replies: 1
    Last Post: June 15th, 2013, 01:06 PM
  5. 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