473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

String to Double or Float

26
hello guys.. i do really need some help here. i have to do the 4 basic mathematical operation by entering 2 numbers with decimal.

example.

1st num: 12.50
2nd num: 6.5

the answer must be in float or double but with my code below, upon entering a double or float numbers it will show this error:

java.lang.NumberFormatException: 2.0
at java.lang.Integer.parseInt(Integer.java:414)
at java.lang.Integer.parseInt(Integer.java:454)
at elvar.promptForInt(elvar.java:46)
at elvar.main(elvar.java:19)
Exception in thread "main"


import java.io.*;

public class elvar {

public static void main (String args[]) {

float a, b;
float sum, diff, quo, prod;
a = promptForInt("Enter 1st integer: ");
b = promptForInt("Enter 2nd integer: ");
sum = a + b;
diff = a - b;
quo = a/b;
prod = a* b;

System.out.println("The total is: " + sum);
System.out.println("The difference is: " + diff);
System.out.println("The quotient is: " + quo);
System.out.println("The product is: " + prod);

}


static float promptForInt(String prompt) {

System.out.print(prompt);
System.out.flush();
String s= "";

BufferedReader ds =
new BufferedReader(new InputStreamReader(System.in));
try {
s = ds.readLine();
} catch (IOException e){
System.out.println(e);
}
return Integer.parseInt(s);
}

i really appreciate some help guys. thanx a lot in advance.
}
Jun 28 '07 #1
2 2350
r035198x
13,262 8TB
hello guys.. i do really need some help here. i have to do the 4 basic mathematical operation by entering 2 numbers with decimal.

example.

1st num: 12.50
2nd num: 6.5

the answer must be in float or double but with my code below, upon entering a double or float numbers it will show this error:

java.lang.NumberFormatException: 2.0
at java.lang.Integer.parseInt(Integer.java:414)
at java.lang.Integer.parseInt(Integer.java:454)
at elvar.promptForInt(elvar.java:46)
at elvar.main(elvar.java:19)
Exception in thread "main"


import java.io.*;

public class elvar {

public static void main (String args[]) {

float a, b;
float sum, diff, quo, prod;
a = promptForInt("Enter 1st integer: ");
b = promptForInt("Enter 2nd integer: ");
sum = a + b;
diff = a - b;
quo = a/b;
prod = a* b;

System.out.println("The total is: " + sum);
System.out.println("The difference is: " + diff);
System.out.println("The quotient is: " + quo);
System.out.println("The product is: " + prod);

}


static float promptForInt(String prompt) {

System.out.print(prompt);
System.out.flush();
String s= "";

BufferedReader ds =
new BufferedReader(new InputStreamReader(System.in));
try {
s = ds.readLine();
} catch (IOException e){
System.out.println(e);
}
return Integer.parseInt(s);
}

i really appreciate some help guys. thanx a lot in advance.
}
1.) Next time please use code tags when posting code.
2.) You use Integer.parseInt for changing a string value to an integer value. To change from a string to a double value, you should use Double.parseDouble and to change from a string to a float you ...well I'm sure you now get the pattern.
Jun 28 '07 #2
You have to modify your promptForInt() method as follow

Expand|Select|Wrap|Line Numbers
  1.  static float promptForInt(String prompt) {
  2.  
  3.     System.out.print(prompt);
  4.     System.out.flush();
  5.     String s= "";
  6.  
  7.     BufferedReader ds = 
  8.     new BufferedReader(new InputStreamReader(System.in));
  9.     try {
  10.     s = ds.readLine();
  11.     } catch (IOException e){
  12.     System.out.println(e);
  13.     }
  14.     float i= Float.parseFloat(s);
  15.  
  16.     return i;
  17.     }
Jun 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
5
by: JustSomeGuy | last post by:
I have a string that looks like: "123.45:123.45" I am using sscanf "%f%*c%f" to extract the two floats and ignore the colon. However there may be more than two floats.... Is there perhaps a...
3
by: Vincent Cantin | last post by:
I have a class defined by a template which needs to "say" its type to the user via string. As an example, here is the class that I want to fix : template<class T> class Container : public...
20
by: Trond Valen | last post by:
Hi! Stupid atof, it returns 0.0 when it tries to parse something like "fish". So I don't know whether the number was really 0 or a string that couldn't be parsed. Is there a better way to do...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
5
by: Thorsten | last post by:
Hi everyone, I am rather new to C# and have a problem that will probably seem trivial to most of you... but I hope you can still help me nevertheless.. Via the comport, I read the result of a...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.