0

I have to input three array elements and then a method should calculate them and return the answer. I only have this :

boolean answer = false;   

final int ARRAY_SIZE = 3;
 int sum[] = new int [ARRAY_SIZE];

 for(int i = 0; i<sum.length;i++){

 sum[i]= Integer.parseInt(JOptionPane.showInputDialog("Please enter a number"));    

 }

 answer = one(sum);

 JOptionPane.showMessageDialog(null,"The sum of the elements is "+answer);
}  
public static boolean one(int[] nums){return(nums[0] + nums[1] + nums[2]);}

}   
1
  • And what is your question or problem? Commented Apr 7, 2016 at 23:39

2 Answers 2

2

At the moment you are returning a boolean from your one method. You should be returning an int.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, didn't even think of it.. Still loads to learn. Thank you :)
1

Maybe my improvements will help you:

public static void main(String[] args) {
    final int ARRAY_SIZE = 3;
    int sum[] = new int[ARRAY_SIZE];
    for (int i = 0; i < sum.length; i++) {

        sum[i] = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number"));

    }
    JOptionPane.showMessageDialog(null, "The sum of the elements is " + one(sum));
}

public static int one(int[] nums) {
    return (nums[0] + nums[1] + nums[2]);
}

1 Comment

Works perfectly, I can see my mistake now. Thank you :)

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.