Results 1 to 8 of 8
  1. #1
    Hillsid3 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Output a Two Decimal Value

    It's suppose to display 12.50 but instead it's displaying 0.0


    Java Code:
    import java.io.*;
    
    public class InputOutput {
    	public static void main(String[] args) {
    		String base="";
    		String height="";
    		
    		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    		try {
    			System.out.print("Input base value = ");
    			base = input.readLine();
    			System.out.print("Input height value = ");
    			height = input.readLine();
    		} catch(IOException e) {
    			System.out.print("Error");
    		}
    		
    		float area = 1/2*(Integer.parseInt(base)* Integer.parseInt(height));
    System.out.println("The Area of the right triangle is "+ area);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Integer division (as in 1/2) chops of the entire fractional part of the quotient, so 1/2 == 0. Either multiply by 0.5 or do the division using floating point numbers, as in 1.0/2.0

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Hillsid3 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    But i need to use the formula 1/2*bh for computing for the area of the triangle. What should I change in the code?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Hillsid3 View Post
    But i need to use the formula 1/2*bh for computing for the area of the triangle. What should I change in the code?
    0.5*bh?

    kind regards,

    Jos (<--- rocket scientist)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Hillsid3 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    no, i mean i still have to use 1/2 not 0.5

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Hillsid3 View Post
    no, i mean i still have to use 1/2 not 0.5
    Who said so? b.t.w. 1/2 equals 0 in Java, so you can't use it here, you have to rewrite your expression a bit ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please re-read the entire text of the 2nd post in this thread: 2nd post

  8. #8
    Babbard is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default

    Both of these will work:

    float area = (1f/2f) * (Integer.parseInt(base)* Integer.parseInt(height));

    float area = (0.5f) * (Integer.parseInt(base)* Integer.parseInt(height));


    The problem is (as JosAH says) is that 1/2 =0.0 (because they are assumed to be int literals) so you need to make them float literals to return 0.5 correctly.

    -Babbard

Similar Threads

  1. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  2. using decimal numbers
    By iminhell in forum New To Java
    Replies: 3
    Last Post: 10-31-2010, 03:47 AM
  3. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 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
  •