Page 1 of 2 12 LastLast
Results 1 to 20 of 32
Like Tree4Likes

Thread: String.Format

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default String.Format

    String SS = String.format("%.3f", d); not working showing error on format ... any help ??? and also Double db = 20.9 not working ....

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    It works for me with double d = 20.9 and float d = 20.9f. Are we supposed to guess what the error is? Ok, let me guess. db vs d (or you forgot to append an f to the float declaration - i.e. 20.9f). For a better answer I will need more info.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: String.Format

    String.format and Double d=20.9 show syntax error

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    I did not get a syntax error. Please show me a Short, Self Contained, Correct Example describing the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: String.Format

    {
    package staticVariable;

    public class StaticVariables {

    public static void main(String[] args) {

    double x = 90.4;
    String S = String.valueOf(x);
    String SS = String.format("%.3f", x); // showing syntex error
    Double xx = 89.45; // syntex error

    }

    }
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: String.Format

    What are the full and exact error messages? We do not read minds.

  7. #7
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: String.Format

    The method format(String, Object[]) in the type String is not applicable for the arguments (String, double).... // for string.format
    Type mismatch: cannot convert from double to Double // for Double ....

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    The outer curly braces aren't necessary and cause an error in my IDE. Remove them and see if it works. It works fine fore me (JDK 1.7). Also, what version of java are you using?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: String.Format

    I am also using 1.7 .. and outer curly brackets are not part of code.. I just added here ....

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    Well, I am not certain what is going on. Here is your submission with some print statements added by me.

    Java Code:
    public class StaticVariables {
       public static void main(String[] args) {
          double x = 90.4;
          String S = String.valueOf(x);
          String SS = String.format("%.3f", x); // showing syntex error
          Double xx = 89.45; // syntex error
          System.out.println(xx);
          System.out.println(SS);
          System.out.println(S);
       }
    }
    No syntax errors. It compiles and runs just fine.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: String.Format

    have a lookkk
    Attached Thumbnails Attached Thumbnails String.Format-untitled.png  

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: String.Format

    That is not 'syntex' error, nor is it a 'syntax' error; that is a runtime error caused by attempting to run something that wouldn't even compile; recompile everything and try again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: String.Format

    0k sorry spell mistake ....but your suggestion is not helpful at all ... I doesn't work even after re-compilation ..

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    Please post all the code.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: String.Format

    According to that screen shot the line 'Double xx= 89.45;' has an error also; I suspect that a very old Java version is selected (pre 1.5, no auto boxing); the OP should check the IDE compiler settings.

    kind regards,

    Jos
    DarrylBurke likes this.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    I asked what version earlier and the OP said 1.7. Interesting to note there is a Thread.sleep() without an apparent try block.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: String.Format

    Quote Originally Posted by jim829 View Post
    I asked what version earlier and the OP said 1.7. Interesting to note there is a Thread.sleep() without an apparent try block.
    An older version compiler can run on a later verion JVM without problems. IDEs like Eclipse and Netbeans allow you to set the version of the compiler; Java 1.4 compilers simply won't generate code for, say, autoboxing. The runtime error (and the wobbly red lines) indicate to me that this is the case.

    kind regards,

    Jos (<--- a bloody laptop in full sunlight is unworkable)
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    I have never had that problem before but then I always keep my compliance levels adjusted to the latest version I have installed. I just did a quick test and changed my Eclipse compliance version to 1.4. And of course I got the syntax error.

    Edit: I also checked the OP's other posts. There was an earlier answer on autoboxing from you and no other posts including anything which would indicate using post 1.4 syntax. Based on your observation a generic type would fail too.

    Regards,
    Jim
    Last edited by jim829; 06-02-2013 at 03:47 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: String.Format

    See? That compiiance level explains it all; that's why I asked the OP to recompile everything so he'd see the compilation errors, but of course that wasn't helpful at all ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: String.Format

    Actually, I'm not certain how recompiling would help. His errors already indicated a syntax error based on his compliance level. It is my experience with Eclipse that compilation is done on the fly assuming no syntax errors. I delete a class and it reappears. Unless he gets some kind of error that tells him his syntax is for a specific release he would have to know what the problem is. If I am missing something here, please let me know.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 1 of 2 12 LastLast

Similar Threads

  1. Pls help to print string in given format
    By know.how in forum New To Java
    Replies: 2
    Last Post: 08-31-2011, 01:43 PM
  2. Help with checking for a certain format in a String
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 04:56 AM
  3. format content of String[]
    By eponcedeleon in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2010, 05:12 AM
  4. String.format not in javadoc
    By ortollj in forum New To Java
    Replies: 3
    Last Post: 11-08-2009, 05:14 PM

Posting Permissions

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