Results 1 to 7 of 7
  1. #1
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default Int to String Issues

    Hello,
    I'm trying to make a program that can calculate pi to several digits using the madhava sequence. The math part is irrelevant to my problem though. I want to calculate pi beyond 16 digits, which is the most precise form of numeric storage (in the variable type 'double'). So, I am going to save it as a string as it goes. For some reason though, I cannot convert the number into a string.
    Java Code:
    //the whole class/program
     package pigenerator;
    
    
    public class Main {
    
        static String pistring = " ";
        static double pinum = 0;
        static int denominator = 1;
        static int limit = 2000000000;
        static boolean add = true;
        static int digits = 0;
    
        public void Main() {
        }
    
        public static void main(String[] args) {
            findpi();
        }
    
        public static void findpi() {// based on madhava series
            while (denominator < limit) {
    
                boolean toggle = add;
                if (add == true) {
                    pinum += 4 / denominator;
                    add = false;
                }
                if (toggle == false) {
                    pinum -= 4/ denominator;
                    add = true;
                }
                denominator += 2;
                digits++;
    
                
                if(digits>4)
                {
                    pistring+=pinum.toString();// here occurs the error
    
                digits=0;
                
                }
                
    System.out.println(pistring);
                
    
            }
    
    
        }
        
        
    }
    The idea is that it calculates a few digits, and then adds it to the string. When I say
    Java Code:
        pistring+=pinum.toString();
    I get an error saying 'double cannot be dereferenced'. Any ideas about what might be causing this error are greatly appreciated. Also, don't worry about the math or anything other than the 'toString()' line. I know that the rest is all fine. Thank you so much.

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

    Default

    A type double is a so called 'primitive' type; primitives don't have any members nor methods so you can't dereference them. Lucky for you the String class has a valueOf( ... ) method that returns a String representation given a (primitive) double value.

    kind regards,

    Jos

  3. #3
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    Easy solution...thanks!

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    JosAH, could you not also use:
    Java Code:
    int x = 5;
    String xAsStr = x+"";
    ...?

    Is that less efficient, or bad practice, or what?

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

    Default

    Quote Originally Posted by Zack View Post
    JosAH, could you not also use:
    Java Code:
    int x = 5;
    String xAsStr = x+"";
    ...?

    Is that less efficient, or bad practice, or what?
    Sure you can but it's less efficient: the compiler creates a StringBuffer for you, adds the "" String and x for you and finally extracts the String from that buffer for you while you could've done it directly by using the method I mentioned in my previous reply (although I'm ofther lazy and use ""+x myself ;-)

    kind regards,

    Jos

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Good to know. I'll keep that in mind, thanks. (""+x is much easier for quick code, but I guess valueOf() should be used in effective programming, specifically where allocation is an issue.)

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

    Default

    Quote Originally Posted by Zack View Post
    Good to know. I'll keep that in mind, thanks. (""+x is much easier for quick code, but I guess valueOf() should be used in effective programming, specifically where allocation is an issue.)
    Yep and I should take my own advice to heart because there are ""+x's in my own code all over the place ;-)

    kind regards,

    Jos

Similar Threads

  1. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  2. String compare issues?
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-21-2009, 07:16 AM
  3. Printing issues...
    By chango77745 in forum New To Java
    Replies: 15
    Last Post: 02-14-2009, 02:01 AM
  4. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM

Tags for this Thread

Posting Permissions

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