2

This is the code that I made.... for the array code

public class main
{
  public static int arraySum(int[] arr)
  {
    int result = 0;
    for (int i = 0; i < arr.length; i++)
    {
      result += arr[i];
    }
    return result;
  }
  public static int arraySumBetter(int [] a)
  {
    int result = 0;
    for (int value : a)
    {
      result += value;
    }
    return result;
  }

  public static void main(String [] args)
  {
    double[] a ={0,2,3,1,0.56};
    System.out.format("%.1f", (double) arraySumBetter(a));
  }
}

when I run it it prints an error out:

typed.java:25: error: incompatible types: double[] cannot be converted to int[] System.out.format("%.1f", (double) arraySumBetter(a));

Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output1 error

I thought to add decimal numbers using double would be work but it didn't work.. Is there any way to fix this? I would appreciate any tips... thanks...

2
  • My english is not that good so if you guys write details i would be appreciated Commented Mar 25, 2017 at 15:32
  • change your arraySumBetter method's parameter type to double Commented Mar 25, 2017 at 15:35

2 Answers 2

1

You can not cast from a type to a whole different non related type. With that said double != int. Instead of casting here

   System.out.format("%.1f", (double) arraySumBetter(a));
                               ^ remove

You could change your methods to accept a double[] array as parameter like this

public static double arraySumBetter(double[] a)
  {
    double result = 0;
    for (double value : a)
    {
      result += value;
    }
    return result;
  }

Note that I changed your primitive type from int to double or else you would lose the decimal points.

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

2 Comments

@W.J What did you do?
@W.J I changed the return type of arraySumBetter to double I forgot to add that.
1

use double as IN and OUT in your function.. see my comments

  public static int arraySum(int[] arr)
  {
    int result = 0;
    for (int i = 0; i < arr.length; i++)
    {
      result += arr[i];
    }
    return result;
  }
  public static double arraySumBetter(double [] a) //<<<<<<<<<<<< IN: double, OUT: also double, else the decimals are lost 
  {
    double result = 0; //<<<<<<<<<< here use double, else you loose decimals
    for (double value : a) //<<<<<<<<<<<<<<<<<<<<<<<<<< value is double, because a is double[]
    {
      result += value;
    }
    return result;
  }

  public static void main(String [] args)
  {
    double[] a ={0,2,3,1,0.56};
    System.out.format("%.1f", (double) arraySumBetter(a));
  }

2 Comments

I was thinking of printing out 2 or 3 digits number like 5.55 than how do i do that?
"%.2f" instead of "%.1f" -> 2 digits

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.