Activity Stream
48,167 MEMBERS
6799 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 5 of 5
  1.     
    #1
    Member

    Default [java] Output a Two Decimal Value

    I wanted display the result with two decimal values? (12.50) but it seems to be displaying 0 instead of 12.50.

    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");
        		}
        		
        		int area = 1/2*(Integer.parseInt(base)* Integer.parseInt(height));
    		System.out.println("The Area of the right triangle is "+ area);
        	}
        }
    Hillside Reviewed by Hillside on . [java] Output a Two Decimal Value I wanted display the result with two decimal values? (12.50) but it seems to be displaying 0 instead of 12.50. import java.io.*; public class InputOutput { public static void main(String args) { String base=""; String height=""; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Rating: 5

  2.   Sponsored Links

  3.     
    #2
    Member
    Change area type to float

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

  4.     
    #3
    Member
    ^The output was:

    Code: 
    The Area of the right triangle is 0.0
    What's wrong?

  5.     
    #4
    Member
    make the following changes

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

  6.     
    #5
    Member
    Quote Originally Posted by Hillside View Post
    ^The output was:

    Code: 
    The Area of the right triangle is 0.0
    What's wrong?
    This is a good time just to think. Your equation is right, so obviously one of the terms is 0. So it would lead to you parsing/casting incorrectly. As 18folder pointed out you cast base and height as integers and you should have casted them as doubles.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java stinks: Turn off your Java browser plugin
    By Loget in forum General Discussion
    Replies: 37
    Last Post: 5th Sep 2012, 07:40 AM
  2. How To Fix Validation Output: 70 Errors
    By BigBang24 in forum Webmaster Discussion
    Replies: 7
    Last Post: 31st Mar 2011, 08:49 AM
  3. Is there a php function to convert text to decimal ncr?
    By Tigger in forum Webmaster Discussion
    Replies: 0
    Last Post: 20th Jan 2011, 07:48 AM
  4. How do I disable output buffering in php?
    By jamilv in forum Server Management
    Replies: 9
    Last Post: 18th Dec 2010, 11:12 PM
  5. [php] Compression | Output Buffer control
    By litewarez in forum Web Development Area
    Replies: 2
    Last Post: 7th Jul 2010, 06:14 PM

Tags for this Thread

BE SOCIAL