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

• September 8th, 2012, 10:30 PM
richyy
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);
}
}
}
• September 9th, 2012, 09:31 AM
Norm
Re: NumberFormat class, rounding decimals (Beginner needs help!)
Quote:

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