Results 1 to 14 of 14
- 11-26-2008, 05:46 PM #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:
- 11-26-2008, 06:18 PM #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"); }
- 11-26-2008, 07:01 PM #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?
- 11-26-2008, 10:04 PM #4
two spaces?
Not sure I understand...i need only 2 spaces after comma...
- 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 :-)
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-26-2008, 10:48 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
oh =) forgot that info. i ment in println().
- 11-26-2008, 11:24 PM #6
hhhmmm...
You mean like:
CJSLJava Code:System.out.print("One space:" + " " + ", now two spaces:" + " " + ". The end!");Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-03-2008, 09:15 AM #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 :).
- 12-03-2008, 09:36 AM #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:
So you can use that method written above in your programJava Code:float y = 9.87654f; float w; w = round(y,3); System.out.println(w);
- 12-03-2008, 03:59 PM #9
or you can attach a DecimalFormat to your print statement.
- 12-03-2008, 09:00 PM #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.
- 12-03-2008, 09:15 PM #11
- 12-03-2008, 09:26 PM #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.
- 12-03-2008, 09:30 PM #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
- 12-04-2008, 12:36 AM #14
Member
- Join Date
- Dec 2008
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
comparing array elements
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-13-2008, 02:33 AM -
Adding elements to an Object Array
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 09-07-2008, 03:55 PM -
How to check whether two elements are available in an array?
By venkatteshb in forum New To JavaReplies: 8Last Post: 08-27-2008, 10:45 PM -
reference to elements in array
By Igor in forum New To JavaReplies: 1Last Post: 12-14-2007, 11:56 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks