Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java printf

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java printf

    This is part of my code. I'm getting "illegalformatconversion" at the printf statement. How can I change the code so that the third thing it print is the votes / total votes? Thanks

        public static void printResults(List<Candidate> c )
        {   
            int total = getTotal(c);
            System.out.println("Candidate              Votes Received           % of Total Votes");
     
     
            for(int x = 0 ; x < c.size(); x++)
            {
            System.out.printf("%-26s %-5d %5.2f  \n",c.get(x).getName(),c.get(x).getVotes(),(c.get(x).getVotes() / total ));
            }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java printf

    I'm guessing because i cant see the rest of your code. Is getVotes() and total ints or Integers? An int divided by an int is still an int. There will be no float value produced. An easy way to trick your program into making it a float is to multiply your values by 1.0.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java printf

    It not allowing me to print at all, it giving me the error at that line. I think what causing is I don't have a conversion format inside the printf for "(c.get(x).getVotes() / total ))" I do have %5.2f but I'm not sure if that work for two values. Thanks

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java printf

    Exactly what i said. Those two values you are dividing are integers not floats so the %5.2f is upset because it is expecting a float.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java printf

    So do I change it to %5.2d? I still get the error on that.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Java printf

    Example: Votes = 10 and Total = 56; Votes / Total = 0...But (Votes * 1.0) / (Total * 1.0) = 0.17857142857

    --- Update ---

    The 5.2 gives you 5 characters prior to decimal and 2 numbers precision after. That doesnt make sense for a "d". You either have to commit to having it round or change your calculation to result in a float.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Java using printf to format.
    By maple1100 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 31st, 2012, 08:29 PM
  2. [SOLVED] printf error
    By Thor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 10:05 PM
  3. How to align text? Printf?
    By shifat96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 20th, 2012, 12:51 PM
  4. about printf please help having errors
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 11:01 PM
  5. The printf() method explanation needed
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 14th, 2010, 12:11 AM