Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: issues with a float

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default issues with a float

    Hi there,

    I have decided to learn java programming, rather than working though a book which i find very dull i have just dived in by writing (trying to write) an application.

    my application is for my other passion in life darts. so basically what the application does at the moment is calculates a players score from 501 down to 0 finishing on a double. this works fine but when i try to calculate the players 1 dart average i'm getting the wrong figure. To test this I did a 9 dart leg and the 1 dart average should be 55.67 however my code comes up with 55.0

    I know that i'm probably missing something really obvious, i'm just at a loss to know what it is?

    here is the code, appologies if anyone thinks its messy, it's the first programming i have done for about 14 years!
    import java.util.Scanner;
     
    public class DartApp {
    	public static void main(String[] args){
    		//create a scanner
    		Scanner scanner = new Scanner(System.in);
     
    		int total = 501;	//to be changed depending on game
    		int nthrows = 0;	//number of throws to work out average
    		float tdartave;		//three dart average calculator
     
    		do{		
    			System.out.print("enter player 1 score: ");
    			int score = scanner.nextInt();
     
    			total = total - score;
    			System.out.println("score is " +total);
     
    			//calculate number of throws
    			if (total>0){
    				nthrows = nthrows +3;}
    			else{
    				System.out.print("which dart was the winning dart 1,2,3 : ");
    				int wdart = scanner.nextInt();
    				nthrows = nthrows + wdart;
    				System.out.println("darts thrown = " +nthrows);}
    		}while (total>0);
     
    		tdartave = (501 / nthrows);
    		System.out.println("three dart average = " +tdartave);
     
    	}
    }

    cheers for any help that can be offered, and sorry if I appear thick
    Mike


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: issues with a float

    You're probably having an issue with integer arithmetic. If variables in an expression are int, the compiler does int arithmethic. For example: 9/5 = 1 or 4/5 = 0
    Use floating point numbers in the expression for the compiler to evaluate an expression as float.
    1.0/2 = 0.5
    or
    (float)1 / 2

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issues with a float

    hmm this is puzzling,

    I have change the line
    tdartave = (501 / nthrows);

    to the following
    tdartave = (501.0 / nthrows);

    and i'm now getting this error
    DartApp.java:48: possible loss of precision
    found : double
    required : float
           tdartave = (501.0 / nthrows);
                             ^
    1 error

    but what i don't understand is tdartave is a float
    Last edited by 2k.; July 30th, 2011 at 11:52 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: issues with a float

    501.0 is a double. Your code reduces its precision to a float.
    There really is no reason to use a float.
    Change your variable to a double.
    Or make the number a float by adding a trailing f : 501.0f

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: issues with a float

    Java will default floating-point-like numbers as doubles (which have more precision than floats). You can force it to use floats by putting an 'f' after the number, or cast the final result to float.

    // either is acceptable
    tdartave = 501.0f / nthrows;
    tdartave = (float)(501.0 / nthrows);

    However, I would just change the type of tdartave to double and make life fun and easy

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: issues with a float

    cheers for the replies
    by changing to
    tdartave = 501.0f / nthrows;

    I get the result 55.666668 which is perfect for the time being, having it rounding down to 55 the three dart average was out by 2.

    I know the calculation was for a one dart average, but i was trying to find where the 2 points had gone

Similar Threads

  1. float based layout manager?
    By deepthought in forum AWT / Java Swing
    Replies: 1
    Last Post: January 1st, 2011, 10:11 PM
  2. [SOLVED] Why is my float automatically rounding and how do i get it to stop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 26th, 2010, 01:41 PM
  3. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  4. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM