Results 1 to 10 of 10
  1. #1
    bigvanilla is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default String to double errors

    Hi, I am trying to cut down the decimals to two, but I get this error(see picture).
    I have try several methods but getting the same errors.

    // Patrik

    Java Code:
    import java.text.DecimalFormat;
    
    public class TestD
    {
    
        public TestD(){
            
            System.out.println("Double value: " + getArea());
        }
    
        public double getArea() {
            
            double dTemp = Math.PI;
            
            DecimalFormat twoDForm = new DecimalFormat("#.##");
            
            String aTemp = twoDForm.format(dTemp);
            
            return Double.valueOf(aTemp.trim()).doubleValue();
        }
    }
    Attached Thumbnails Attached Thumbnails String to double errors-error-string-double.jpg  

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    I runs fine for me; sorry, that thumbnail is too small for me; what is it protesting about? Is "3.14" (with a dot) or "3,14" (with a comma).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bigvanilla is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    I am using BlueJ.

    Here is the error:

    java.lang.NumberFormatException: For input string: "3,14"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    at java.lang.Double.valueOf(Double.java:475)
    at TestD.getArea(TestD.java:30)
    at TestD.<init>(TestD.java:19)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:790 )

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bigvanilla View Post
    I am using BlueJ.

    Here is the error:

    java.lang.NumberFormatException: For input string: "3,14"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    See the comma instead of the dot? You formatter is using a locale that has a comma instead of a dot for separating the decimal expansion of the fraction; here 3,14 instead of 3.14. Check the used Locale and possibly use another one.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bigvanilla is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks it is solved now :-)

  6. #6
    bigvanilla is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    More problem I need help for. This is nearly the same code with two extra variables.

    Java Code:
    import java.text.DecimalFormat;
    
    public class TestD
    {   
        int width;
        int height;
    
        public TestD(){
            width = 20;
            height = 30;
            
            System.out.println("Double value: " + getArea(width, height));
        }
    
        public double getArea(int width, int height) {
            
            double dTemp = Math.PI * (double)width * (double)height;
            
            DecimalFormat twoDForm = new DecimalFormat("#,##");
            
            String aTemp = twoDForm.format(dTemp);
            
            return Double.valueOf(aTemp.trim()).doubleValue();
        }
    }
    I get this error message:
    Java Code:
    [COLOR="DarkRed"]java.lang.NumberFormatException: For input string: "18 85"
    	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
    	at java.lang.Double.valueOf(Double.java:475)
    	at TestD.getArea(TestD.java:29)
    	at TestD.<init>(TestD.java:14)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at bluej.runtime.ExecServer$3.run(ExecServer.java:790)[/COLOR]
    It is complaining on the last row of the code with the return.

    Anyone who can help me with this?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    In your current code, your DecimalFormat object is superfluous and doing nothing but helping to cause an error. If the method takes in two numbers and then returns a number (a double), why convert it to a String? If however your goal is to learn to use a DecimalFormat object to convert a double to a String and then back again, then you'll want to use DecimalFormat's parse method as well:

    Java Code:
    import java.text.DecimalFormat;
    import java.text.ParseException;
    
    public class TestD {
       int width;
       int height;
    
       public TestD() throws ParseException {
          width = 20;
          height = 30;
          System.out.println("Double value: " + getArea(width, height));
       }
    
       public double getArea(int width, int height) throws ParseException {
          double dTemp = Math.PI * (double) width * (double) height;
          DecimalFormat twoDForm = new DecimalFormat("#,##");
          String aTemp = twoDForm.format(dTemp);
          System.out.println("aTemp is " + aTemp);
          //!!return Double.valueOf(aTemp.trim()).doubleValue();
          return twoDForm.parse(aTemp).doubleValue();
       }
       
       public static void main(String[] args) {
          try {
             new TestD();
          } catch (ParseException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
    }

  8. #8
    bigvanilla is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi, I did run your code and got no errors :-)

    But, I am still not get the double value with two decimals. Actually I do not care if I do need to convert it to a string and then back to double.
    It was just one solution I found on the net. (I do not know if this could be done different?)

    The main reson is to just get a double value with only two decimals.

    // Patrik

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bigvanilla View Post
    Hi, I did run your code and got no errors :-)

    But, I am still not get the double value with two decimals.
    The method is supposed to return a double, and doubles really don't have a number of decimals -- rather a String representation of a double does. So don't worry about the number of decimals of the number until you want to display the String.

    Actually I do not care if I do need to convert it to a string and then back to double.
    You should worry about this as it messes with your result for no reason at all and can introduce errors.

    The main reson is to just get a double value with only two decimals.
    Again, use a DecimalFormat to create a String from the double that has two decimals, but again only do this when you want to display the double as a String.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    I really don't understand where that space comes from in "18 85"; 20*30*PI == 1884.9555921539 which figures but I think the OP's locale is still a bit strange ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. parse string into 2 double variables
    By woodsie2523 in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 02:46 PM
  2. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 03:46 AM
  3. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 07:01 PM
  4. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 09:03 PM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 04:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •