1
Language: Java 
OS: Windows 
IDE: NetBeans 8.0.2

I'm very new at Java and am attempting to teach myself using online resources so I apologize for the newb question and thank's in advance for the help.

My goal: Create a class. Assign a value to a variable. Create a function. Create an object. Call the function to display the variable.

Problem: "error: cannot find symbol test1.printResult(result);"

package usingoperators;

class MathDemo 
{
    // create integer variable and initiate it to 3
    int result = 1 + 2; //result is now 3

    // create printResult function with result parameter
    void printResult(int result) // result is passed to printResult function
    {
        System.out.println(result); // prints result
    }
}    
public class UsingOperators 
{
   public static void main(String[] args) 
    {      
        MathDemo test1 = new MathDemo(); // creates test1 object
        test1.printResult(result); // calls the printResult function to print result
    }
}

My guess is that I'm not passing "result" to printResult(); correctly.

2
  • 2
    Remove the parameter int result from printResult(int result) as you don't need it. Then in your main method write test1.printResult(); instead. Commented Sep 4, 2015 at 8:12
  • I suggest you to read a book about Java. Core Java by Cay Horstmann is one of my favorites: amazon.com/Core-Java-I--Fundamentals-9th/dp/0137081898 Commented Sep 4, 2015 at 8:13

3 Answers 3

3

There is no variable result within your main method, so Java does not know what to pass to printResult(). You should define a result variable in your main method locally, so you can pass it to the method (rather call it method in OOP, not function).

I'd also implement an add() method, so you can pass two numbers and it returns the sum.

Class for doing math:

package usingoperators;

public class MathDemo 
{

    // Calculate a + b and return result
    public int add(int a, int b) {
        return a + b;
    }

    // create printResult function with result parameter
    public void printResult(int result) // result is passed to printResult function
    {
        System.out.println(result); // prints result
    }
}    

Class to use MathDemo:

package usingoperators;

public class UsingOperators 
{
   public static void main(String[] args) 
    {
        MathDemo test1 = new MathDemo(); // creates test1 object

        int result = test1.add(1, 2); // Calculate 1 + 2 and store result in variable
        test1.printResult(result); // pass result to method to print it
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is no result variable in your UsingOperators class. You have to create this variable before passing it to the MathDemo printResult(...) method. For example:

public class UsingOperators{

    public static void main(String[] args){
        int reuslt = 1;
        MathDemo test1 = new MathDemo(); // creates test1 object
        test1.printResult(result); // calls the printResult function to print result
    }
}

3 Comments

The goal is apparently to print the result field of the MathDemo object. Not to pass an additional result to the method.
@JBNizet the title of the question is: Passing data to function
If that's how you read the question, then you should also tell the OP that the field result in MathDemo is useless. Otherwise, he will rightfully wonder why result must be defined twice.
1

You are giving result to the function as a parameter but result is not known in the main method. Try this.

   public static void main(String[] args) 
{
    MathDemo test1 = new MathDemo(); 
    int result = 1 + 2; 
    test1.printResult(result); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.