EdXX
EdXX

Reputation: 890

java math.sqrt from array element

I've got two problems. Please find my code attached below:

public class Numbers {

    public static void main(String[] args) {
        double[] tab = { 9.0, 27, 5, 77 };
        System.out.println("array size: " + tab.length);

        for (int y = 0; y < tab.length; y++) {
            System.out.printf("%d", (int) tab[y]);
            System.out.print("    ");
            System.out.print(Math.sqrt(y));
            System.out.println();
        }

        // for(double i:tab){
        // System.out.println(tab[(int) i]);
        // }

    }
}

And now

1) my first problem is that I have some numbers in my array tab and then in the FOR loop I want to display in each line the element and its square root. But for the first element i get 0.0 as a square root. why? The other results are wrong as well to me.

my outcome:

array size: 4
9    0.0
27    1.0
5    1.4142135623730951
77    1.7320508075688772

2) second problem is with my for each loop which is commented. But when you uncomment it the it doesnt work because I get an error. The output:

array size: 4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9

What did I do wrong with this for each loop? Why eclipse shows that I ask about 9-th element?

Thank you for help in advance - all answers are appreciated :)

Upvotes: 2

Views: 1445

Answers (4)

Abdelhak
Abdelhak

Reputation: 8387

The problem is you try to retrieve data from array in the index out of bounds, ty to change this:

    for(double i : tab){ 
       System.out.println(tab[(int) i]);
      }

With this:

  for(double i : tab){
    System.out.println(i);
   }

Upvotes: 1

Graphican0
Graphican0

Reputation: 167

First Problem: You are not using the elements from the array, but your loop parameter:

public static void main(String[] args) {
        double[] tab = { 9.0, 27, 5, 77 };
        System.out.println("array size: " + tab.length);

        for (int y = 0; y < tab.length; y++) {
            System.out.printf("%d", (int) tab[y]);
            System.out.print("    ");
            System.out.print(Math.sqrt(tab[y]));
            System.out.println();
        }
}

(this would be the correct code)

Second Problem: You are using an foreach loop, so there is no need to use an index to get an array element:

for(double i : tab){
    System.out.println(i);
}

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500375

But for the first element i get 0.0 as a square root. why?

Because you're not printing out the square root of the element - you're printing out the square root of the index. This:

System.out.print(Math.sqrt(y));

should be:

System.out.print(Math.sqrt(tab[y]));

Upvotes: 2

Todd
Todd

Reputation: 31690

But for the first element i get 0.0 as a square root. why?

Because you are getting the square root of the loop index, not the value stored at the index!

System.out.print(Math.sqrt(y));

Should be changed to

System.out.print(Math.sqrt(tab[y]));

second problem is with my for each loop which is commented. But when you uncomment it the it doesnt work because I get an error.

You are just misinterpreting the purpose of the "enhanced for loop":

 for(double i:tab){
     // System.out.println(tab[(int) i]);  // Wrong! 
     System.out.println(i);
 }

That's what you want. This loop says: "Give me every element of tab, one at a time, and store it in i for this loop". Even though you've named your variable i, it is not a loop index, it's the actual value held in the array.

Upvotes: 3

Related Questions