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 8 of 8

Thread: "%10.2f" Formatting

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question "%10.2f" Formatting

    I am trying to run a program for my class where I have to have the average computed and the teacher wants to see 2 numbers past the decimal point. I know what I need to add, however I have no idea where to put it to make it work properly... This is the first program that I have had to do on my own and for the most part I think I have it...

    import javax.swing.JOptionPane;
    public class Modeling_001C_4Loops {
    public static void main(String[] args) {

    // Define height and weight and assign values
    final int HEIGHT = 70;
    final double WEIGHT = 110.4999;

    // Prompt user for height in inches
    String applicantsHeightString = JOptionPane.showInputDialog("Enter applicants height (in inches)");
    double applicantsHeight = Double.parseDouble(applicantsHeightString);

    if (applicantsHeight >= HEIGHT) {
    JOptionPane.showMessageDialog(null, "Send applicant to get Weighed");
    }

    else if (applicantsHeight < HEIGHT) {
    JOptionPane.showMessageDialog(null, "Send her home...does not qualify");
    }
    // Prompt user of Weight in pounds
    if (applicantsHeight >= HEIGHT) {
    String applicantsWeightString = JOptionPane.showInputDialog("Enter Applicants Weight (in pounds)");
    double applicantsWeight = Double.parseDouble(applicantsWeightString);

    if (applicantsWeight <= WEIGHT) {
    JOptionPane.showMessageDialog(null, "Congratulate her and Send to the first Photographer");
    }
    else if (applicantsWeight > WEIGHT) {
    JOptionPane.showMessageDialog(null, "Send her home! Does not meet weight requrements!");
    }

    // Define variables for Photographer 1
    if (applicantsHeight >= HEIGHT && applicantsWeight <= WEIGHT) {
    String photographer1String = JOptionPane.showInputDialog("Enter 1st Photographers Score");
    double photographer1 = Double.parseDouble(photographer1String);

    if (photographer1 >= 1 && photographer1 <= 10)
    JOptionPane.showMessageDialog(null, "Send to 2nd Photographer!");

    else if (photographer1 == 0) {
    JOptionPane.showMessageDialog(null, "No Pictures! IDon't want to break the camera" +
    "...it's my favorite");
    }
    else if (photographer1 >= 10.01) {
    JOptionPane.showMessageDialog(null, "Invalid Input! Please try again.");
    }

    // Define variables for Photographer 2
    if (photographer1 >= 1 && photographer1 <= 10) {
    String photographer2String = JOptionPane.showInputDialog("Enter 2nd Photographers Score");
    double photographer2 = Double.parseDouble(photographer2String);

    if (photographer2 >= 1 && photographer2 <= 10)
    JOptionPane.showMessageDialog(null, "Send to 3rd Photographer!");

    else if (photographer2 == 0) {
    JOptionPane.showMessageDialog(null, "What was the last guy thinking?" +
    " Send her home! NOW!!");
    }
    else if (photographer2 >= 10.01) {
    JOptionPane.showMessageDialog(null, "Invalid Input! Please try again.");
    }

    // Define variables for Photographer 3
    if (photographer2 >= 1 && photographer2 <= 10) {
    String photographer3String = JOptionPane.showInputDialog("Enter 3rd Photographers Score");
    double photographer3 = Double.parseDouble(photographer3String);

    if (photographer3 >= 1 && photographer3 <= 10)
    JOptionPane.showMessageDialog(null, "Calculate Average Score");

    else if (photographer3 == 0) {
    JOptionPane.showMessageDialog(null, "Waste of my time. How did she make it this far?");
    }
    else if (photographer3 >= 10.01) {
    JOptionPane.showMessageDialog(null, "Invalid Input! Please try again");
    }

    // Define average and assign a value
    double averageScore = (photographer1 + photographer2 + photographer3) / 3;

    if (averageScore <= 7.9 && photographer3 != 0) {
    JOptionPane.showMessageDialog(null, "%10.2f" + " Average " + averageScore + "!" +
    " Too high risk, not worth spending your money!!");
    }
    else if (averageScore >= 8 && averageScore <=8.7) {
    JOptionPane.showMessageDialog(null, "%10.2f" + " Average " + averageScore + "!" +
    " Medium to High risk....but could make you money. Eventually!");
    }
    else if (averageScore >= 8.8 && averageScore <= 9.3) {
    JOptionPane.showMessageDialog(null, "%10.2f" + " Average " + averageScore + "!" +
    " Low to Medium risk....has a pretty good chance to make you money!");
    }
    else if (averageScore >= 9.4 && averageScore <= 9.9) {
    JOptionPane.showMessageDialog(null, "%10.2f" + " Average " + averageScore + "!" +
    " Low risk to good investment....Excelant chance you will make money " +
    "bring her on board!");
    }
    else if (averageScore == 10) {
    JOptionPane.showMessageDialog(null, "%10.2f" + " Average " + averageScore + "!" +
    " Get her to sign a contract before she realizes how much she could take you for!!!");
    }
    }
    }
    }
    }
    }
    }



    This is where I thought they would go, but all it does is add it as text in the message box, which is not what I want it to do

    Any suggestions would be great!
    Last edited by Kattracks32; February 9th, 2013 at 09:16 PM. Reason: Changed the color


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: "%10.2f" Formatting

    Use String.format("...", ...) for this.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "%10.2f" Formatting

    okay...I have no idea how to do that, or where to put it.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: "%10.2f" Formatting

    You would use it anywhere you'd like to produce a formatted String from data. Use it same as if you were using System.out.printf(...) only rather than printing the String out to the standard out, it *returns* the String for you to use anywhere you'd like.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "%10.2f" Formatting

    // Define average and assign a value
    double averageScore = (photographer1 + photographer2 + photographer3) / 3;
    String.format("%10.2f", 12.22); // I added it here, but it made no difference. Am I using it wrong?

    if (averageScore <= 7.9 && photographer3 != 0) {
    JOptionPane.showMessageDialog(null, "Send Her Home!");
    }
    else if (averageScore >= 8 && averageScore <=8.7) {

    JOptionPane.showMessageDialog(null, "Average " + averageScore + "!" +
    "Too high risk, not worth spending your money!!");
    }
    else if (averageScore >= 8.8 && averageScore <= 9.3) {
    JOptionPane.showMessageDialog(null, " Average " + averageScore + "!" +
    " Low to Medium risk....has a pretty good chance to make you money!");
    }
    else if (averageScore >= 9.4 && averageScore <= 9.9) {
    JOptionPane.showMessageDialog(null, " Average " + averageScore + "!" +
    " Low risk to good investment....Excelant chance you will make money " +
    "bring her on board!");
    }
    else if (averageScore == 10) {
    JOptionPane.showMessageDialog(null, " Average " + averageScore + "!" +
    " Get her to sign a contract before she realizes how much she could take you for!!!");

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: "%10.2f" Formatting

    You will want to assign what is returned by the method call to a String variable, and then use that variable.

    double averageScore = (photographer1 + photographer2 + photographer3) / 3;
    String formatedAverageScore = String.format("%10.2f", 12.22);  // now you can use this String where desired

    Also please check the forum FAQ's about use of code tags as it will make it much easier for others to read your posted code.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "%10.2f" Formatting

    Worked Great!
    Thanks

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: "%10.2f" Formatting

    You're welcome! Also note that you can use it right in your JOptionPane if desired. i.e.,

    JOptionPane.showMessageDialog(Null, String.format("The average score is %10.2f which is pretty awesome", averageScore));

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 08:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 02:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 08:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM