# PROBLEM - calculating with array elements

• 11-26-2008, 06:46 PM
ella
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:
• 11-26-2008, 07:18 PM
serjant
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:

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");     }```
• 11-26-2008, 08:01 PM
ella
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?
• 11-26-2008, 11:04 PM
CJSLMAN
two spaces?
Quote:

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
• 11-26-2008, 11:48 PM
ella
oh =) forgot that info. i ment in println().
• 11-27-2008, 12:24 AM
CJSLMAN
hhhmmm...
You mean like:
Code:

`System.out.print("One space:" + " " + ", now two spaces:" + "  " + ". The end!");`
CJSL
• 12-03-2008, 10:15 AM
ella
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 :).
• 12-03-2008, 10:36 AM
serjant
This code will round your float:

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:

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
• 12-03-2008, 04:59 PM
xcallmejudasx
or you can attach a DecimalFormat to your print statement.
• 12-03-2008, 10:00 PM
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.
• 12-03-2008, 10:15 PM
xcallmejudasx
Quote:

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-03-2008, 10:26 PM
timkd127
haha u kno i noticed that after i posted it but was too lazy in that moment to edit it.
• 12-03-2008, 10:30 PM
xcallmejudasx
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
• 12-04-2008, 01:36 AM
timkd127
yea, plus with the IDE's now u can just run ur mouse over a variable and it shows u where it occurs.