-5

I have a method which returns [2] array, each value returned are doubles:

public class additions;
{

    public double [] Number ()
    double [] z;
    z = new double [2]

    z[0]= 12+5;
    z[1]= 58-8; 
    return z;// I would assumed values returned are stored in "Number";
}

Then in the main class, I have

     additions  calculus_1 = new additions();
     System.out.print(Arrays.deepToString());//neither of them seems to be working
     System.out.print([0].Stringform());
     System.out.print(calculus_1.Number());

I am unable to print the answer, Can you use a loop? where should I printing out the answer in the main class?

I am looking for a dynamic since I would be changing the calculation for Z.

Jens
  • 67,715
  • 15
  • 98
  • 113
nelectron
  • 1
  • 1
  • 11
    Please concentrate a bit more on providing valid code. The code you've given for `additions` has a number of errors. – Jon Skeet Mar 17 '15 at 13:17
  • 1
    My point is not helping to find the solution but please use the Java Coding Standards. Class names are always written with a capital letter -> Additions. I would also suggest to name your variables more clear. Z is not a good variable name at all. – Rubinum Mar 17 '15 at 13:21
  • You should learn the basics of Java first. – Bubletan Mar 17 '15 at 13:21

4 Answers4

2

Your method returns the array, so to print it, you need :

System.out.print(Arrays.toString(calculus_1.Number()));

I'm not sure if you had typos while copying your code to the question, but your class is missing a couple of braces and has some semi-colons in the wrong place.

This should work:

public class additions
{
  public double [] Number ()
  {
    double [] z;
    z = new double [2];

    z[0]= 12+5;
    z[1]= 58-8; 
    return z;
  }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
0

Since you are storing a double, you should use:

String.valueOf(z[0]);

Also, you could use a for-loop, like this:

for(int i = 0; i < z.length; i++) {
   System.out.println(String.valueOf(z[i]));
}
Rubinum
  • 547
  • 3
  • 18
0

This should print the array. But I'm not realy sure if this is your problem?

        double[] d_array = new double[2];
        d_array[0] = 1;
        d_array[1] = 2;
        String printArray = "";
        for (double d : d_array) {
            printArray = printArray + ", " + d;
        }
        System.out.println(printArray.substring(2));
Marvin
  • 55
  • 11
0
 System.out.print(Arrays.deepToString());//neither of them seems to be working
 System.out.print([0].Stringform());

This is dead code. It does nothing. In the first line you are asking the class Arrays to do something?? I'm not sure what you want to do there.

Also change the name of the Number() function it might have conflicts with Java. Also brackets and semicolons required:

public double [] arrayStuff() {
    double [] z;
    z = new double [2];

    z[0]= 12+5;
    z[1]= 58-8; 
    return z;
}

Here is my assumption of what you want:

additions  calculus_1 = new additions();
double[] result = calculus_1.arrayStuff();

for (int i = 0; i < result.length; i++) {
    System.out.println(result[i]);
}
Code Whisperer
  • 1,041
  • 8
  • 16