String to Double or Float

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Synapse
    New Member
    • Dec 2006
    • 26

    String to Double or Float

    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.Numbe rFormatExceptio n: 2.0
    at java.lang.Integ er.parseInt(Int eger.java:414)
    at java.lang.Integ er.parseInt(Int eger.java:454)
    at elvar.promptFor Int(elvar.java: 46)
    at elvar.main(elva r.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("E nter 1st integer: ");
    b = promptForInt("E nter 2nd integer: ");
    sum = a + b;
    diff = a - b;
    quo = a/b;
    prod = a* b;

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

    }


    static float promptForInt(St ring prompt) {

    System.out.prin t(prompt);
    System.out.flus h();
    String s= "";

    BufferedReader ds =
    new BufferedReader( new InputStreamRead er(System.in));
    try {
    s = ds.readLine();
    } catch (IOException e){
    System.out.prin tln(e);
    }
    return Integer.parseIn t(s);
    }

    i really appreciate some help guys. thanx a lot in advance.
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Synapse
    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.Numbe rFormatExceptio n: 2.0
    at java.lang.Integ er.parseInt(Int eger.java:414)
    at java.lang.Integ er.parseInt(Int eger.java:454)
    at elvar.promptFor Int(elvar.java: 46)
    at elvar.main(elva r.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("E nter 1st integer: ");
    b = promptForInt("E nter 2nd integer: ");
    sum = a + b;
    diff = a - b;
    quo = a/b;
    prod = a* b;

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

    }


    static float promptForInt(St ring prompt) {

    System.out.prin t(prompt);
    System.out.flus h();
    String s= "";

    BufferedReader ds =
    new BufferedReader( new InputStreamRead er(System.in));
    try {
    s = ds.readLine();
    } catch (IOException e){
    System.out.prin tln(e);
    }
    return Integer.parseIn t(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.parseIn t for changing a string value to an integer value. To change from a string to a double value, you should use Double.parseDou ble and to change from a string to a float you ...well I'm sure you now get the pattern.

    Comment

    • KishoreM
      New Member
      • Jun 2007
      • 12

      #3
      You have to modify your promptForInt() method as follow

      [CODE=java] static float promptForInt(St ring prompt) {

      System.out.prin t(prompt);
      System.out.flus h();
      String s= "";

      BufferedReader ds =
      new BufferedReader( new InputStreamRead er(System.in));
      try {
      s = ds.readLine();
      } catch (IOException e){
      System.out.prin tln(e);
      }
      float i= Float.parseFloa t(s);

      return i;
      }[/CODE]
      Last edited by r035198x; Jun 28 '07, 08:40 AM. Reason: added the missing code tags

      Comment

      Working...