I'm trying to print out the value of adding between an integer and a double, however, one of the values is extremely long - What is the explanation for this as everything else seems fine? For example:
public static void main(String[] args) {
double[] b = new double[] {
0.62,
0.54,
0.78
};
int[] a = new int[] {
2,
5,
7
};
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.println(b[i] + a[j]);
}
}
}
#output:
2.62
5.62
7.62
2.54
5.54
7.54
2.7800000000000002 //Why is this so long, and where did the last 2 come from?
5.78
7.78
And secondly, when I try to store the above into a double array, why won't it output correctly?
public static void main(String[] args) {
double[] b = new double[] {
0.62,
0.54,
0.78
};
int[] a = new int[] {
2,
5,
7
};
double[][] c = new double[][] {};
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
c[i][j] = b[i] * a[j];
System.out.println(c[i][j]);
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at test.project1/tested.project.testLoop.main(testLoop.java:17)