Results 1 to 14 of 14
  1. #1
    ella is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default 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. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    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. #3
    ella is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    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. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    ella is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    oh =) forgot that info. i ment in println().

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    You mean like:
    Java Code:
    System.out.print("One space:" + " " + ", now two spaces:" + "  " + ". The end!");
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    ella is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    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. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    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. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    or you can attach a DecimalFormat to your print statement.

  10. #10
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    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. #11
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by timkd127 View Post
    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. #12
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    haha u kno i noticed that after i posted it but was too lazy in that moment to edit it.

  13. #13
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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. #14
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    yea, plus with the IDE's now u can just run ur mouse over a variable and it shows u where it occurs.

Similar Threads

  1. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  2. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM
  3. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  4. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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