Java: How to Take input of Float type variables and then add it??

In my last post I have shared about  How to add Integer numbers in Java.

Here I have shown how to add and work with float type variables in java.

In the given example I have taken two float type variable as input and then add it.

 float variable java

Code Example

import java.io.*;

class FloatDemo

{

public static void main(String args[])

{

float floatNumber=0.0f;

float floatNumber1=0.0f;

float sumOf=0.0f;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

System.out.println(“\n”);

System.out.println(“************************************”);

System.out.println(“\n”);

System.out.println(“Enter 1st Floating point input:”);

floatNumber=Float.valueOf(br.readLine()).floatValue();

System.out.println(“\n”);

System.out.println(“Enter 2nd Floating point input:”);

floatNumber1=Float.valueOf(br.readLine()).floatValue();

System.out.println(“\n”);

sumOf=floatNumber+floatNumber1;

System.out.println(“After Addition of the 2 float  number =”+sumOf);

System.out.println(“\n”);

System.out.println(“************************************”);

}

catch(Exception e)

{

Exception newEx=new Exception(“Error at:”+new java.util.Date()+””,e);

newEx.printStackTrace();

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: