Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert String input to Float ? 1

Status
Not open for further replies.

rpk2006

Technical User
Apr 24, 2002
225
IN
Hi,

Please see my following code. At line 9, I am accepting user input, but I want to convert into float for calculation at line 11. How to do that ? I tried to repalce "String f" to "float f" at line 9, but it is giving error.

------------------------------------------------------------
package myprojects.temperature;

import java.io.*;

1 class Temperature
2 {
3 public static void main(String args[]) throws IOException
4 {
5 BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));

6 System.out.println("This program converts Temperature in Fahrenheit to Celsius");
7 System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

8 System.out.println("Enter temperature in fahrenheit:");
9 String f = myIn.readLine();

10 // Calculation
11 float c = (f - 32) / 1.8;


12 // Display Result
13 System.out.println("Temparature in Celsius: " + c);
14 }
15 }
-------------------------------------- There is always a new solution for the same problem.

Anonymous
 
It's in the Javadoc for Float:

static float parseFloat(String s)
Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top