# Thread: PROBLEM - calculating with array elements

1. Member
Join Date
Nov 2008
Posts
4
Rep Power
0

## PROBLEM - calculating with array elements

hey :) i'm a girl who's studying at the university and i have trouble with java. i hope someone can help. i have 3 arrays. 2 of them are with given elements. elements for third one i have to calculate (distance / speed = time).

int a=4; //length for all
int[]b={100, 150, 138, 196}; //distance
int[]c= {70, 110, 96, 87}; //speed
double[]d=new double[a]; //empty array for times

for(int i=0;i<4;i++){
System.out.print(b[i]+"km ");
System.out.print(c[i]+"km/h ");
d[i]=b[i]/c[i];
System.out.println(d[i]+"h");
}

and the problem is, it prints correctly the distance and speed but when it should calculate the time, then it gives me some kinda rounded result. for example 100/70=1.0 and 196/87=2.0 instead of 1.42 and 2.25... what is wrong?:confused:

2. Hey,i am a boy,wanna tell you that you have a problem with casting, when you calculate division with integers,and the result should be asigned into double value,so cast the result to double,it should be this way:

Java Code:
```     int a=4; //length for all
int[]b={100, 150, 138, 196}; //distance
int[]c= {70, 110, 96, 87}; //speed
double[]d=new double[a]; //empty array for times

for(int i=0;i<a;i++){
System.out.print(b[i]+"km ");
System.out.print(c[i]+"km/h ");
d[i]=(double)b[i]/c[i];
System.out.println(d[i]+"h");
}```

3. Member
Join Date
Nov 2008
Posts
4
Rep Power
0
wow, and that was all?... lol. i was thinking like crazy. works fine now :P. thanx a lot.
and now i have one more question :D. i need only 2 spaces after comma but can't find a simple way how to do it :/. i've found only complicated methods. is there a simple way anyway?

4. ## two spaces?

i need only 2 spaces after comma...
Not sure I understand...
• Two spaces after a comma in a println()?
• Two spaces after a comma in an array?
• Two spaces after a comma in _____? (fill in in blank space :-)

CJSL

5. Member
Join Date
Nov 2008
Posts
4
Rep Power
0
oh =) forgot that info. i ment in println().

6. ## hhhmmm...

You mean like:
Java Code:
`System.out.print("One space:" + " " + ", now two spaces:" + "  " + ". The end!");`
CJSL

7. Member
Join Date
Nov 2008
Posts
4
Rep Power
0
if i calculate 100/170=0.588235294... but i don't want to see that on screen. 2 spaces after comma i mean i wanna see 0.59 instead of that long row of numbers :).

8. This code will round your float:

Java Code:
``` /**
* Round a double value to a specified number of decimal
* places.
*
* @param val the value to be rounded.
* @param places the number of decimal places to round to.
* @return val rounded to places decimal places.
*/
public static double round(double val, int places) {
long factor = (long)Math.pow(10,places);

// Shift the decimal the correct number of places
// to the right.
val = val * factor;

// Round to the nearest integer.
long tmp = Math.round(val);

// Shift the decimal the correct number of places
// back to the left.
return (double)tmp / factor;
}

/**
* Round a float value to a specified number of decimal
* places.
*
* @param val the value to be rounded.
* @param places the number of decimal places to round to.
* @return val rounded to places decimal places.
*/
public static float round(float val, int places) {
return (float)round((double)val, places);
}```

Then call the method like this:

Java Code:
```float y = 9.87654f;
float w;
w = round(y,3);
System.out.println(w);```
So you can use that method written above in your program

9. or you can attach a DecimalFormat to your print statement.

10. Member
Join Date
Dec 2008
Posts
41
Rep Power
0
Hey, also a boy here haha.
Just a suggestion... make
int a=4;
final int a=4;
just a style thing that alot of proffessors and bosses like.

11. Originally Posted by timkd127
Hey, also a boy here haha.
Just a suggestion... make
int a=4;
final int a=4;
just a style thing that alot of proffessors and bosses like.
He means final int A = 4 (since final objects are generally denoted in all caps)

lol sorry just nit picking

12. Member
Join Date
Dec 2008
Posts
41
Rep Power
0
haha u kno i noticed that after i posted it but was too lazy in that moment to edit it.

13. ya I hate writing like that. Specially when I have 50+ final Strings with names like CMD_PROPERTIES_EDIT_VIEW and my caps lock button is broken. Pain in the ass since double clicking selects the entire thing and not just properties or whatever when I need to edit

14. Member
Join Date
Dec 2008
Posts
41
Rep Power
0
yea, plus with the IDE's now u can just run ur mouse over a variable and it shows u where it occurs.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•