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

Thread: NumberFormat class, rounding decimals (Beginner needs help!)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NumberFormat class, rounding decimals (Beginner needs help!)

    Hey guys I'm a beginner at java and taking an online course this semester that I'm currently struggling on. I'd appreciate it if you guys could help me with my code. I need to use the NumberFormat class to round the average score to one decimal place before displaying it at the end of the program. In my book I followed the NumberFormat example, but I keep getting errors and I'm unsure why my project isn't running. I'm also unsure of where I should be putting the NumberFormat class. If someone could help me I would really appreciate it, thank you.

    import java.util.Scanner;

    public class TestScoreApp
    {
    public static void main(String[] args)
    {
    // display operational messages
    System.out.println("Please enter test scores that range from 0 to 100.");
    System.out.println("To end the program enter 999.");
    System.out.println(); // print a blank line

    // initialize variables and create a Scanner object
    double scoreTotal = 0;
    int scoreCount = 0;
    int testScore = 0;
    Scanner sc = new Scanner(System.in);

    // get a series of test scores from the user
    while (testScore != 999)
    {
    // get the input from the user
    System.out.print("Enter score: ");
    testScore = sc.nextInt();

    // accumulate score count and score total
    if (testScore <= 100)
    {
    scoreCount ++;
    scoreTotal += testScore;
    }
    else if (testScore != 999)
    System.out.println("Invalid entry, not counted");
    }

    // display the score count, score total, and average score
    double averageScore = scoreTotal / scoreCount;
    String message = "\n" +
    "Score count: " + scoreCount + "\n"
    + "Score total: " + scoreTotal + "\n"
    + "Average score: " + averageScore + "\n";
    {
    if (testScore == 999)

    double miles = scoreTotal / scoreCount;
    NumberFormat number = NumberFormat.getNumberInstance();
    number.setMaximumFractionDigits(1);
    String mileString = number.format(miles);
    System.out.println(message);
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: NumberFormat class, rounding decimals (Beginner needs help!)

    I keep getting errors
    Please post the full text of the error messages.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: August 29th, 2011, 07:32 PM
  2. [SOLVED] Processing Decimals <1 & >0
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2011, 01:53 PM
  3. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM
  4. Convert Decimals to Octals using only If/Else
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 9th, 2011, 11:58 AM
  5. Rounding an int. Help please
    By Akim827 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 18th, 2010, 11:36 PM