EDIT:
Thanks, everyone.
I'm using the NumberFormat class (or method, I'm still getting used to the terminology), but when I compile the program I get a "cannot find symbol" pointing at the decimal in "NumberFormat.getPercentInstance();" but I don't underhand why. Here's the code:
import java.util.Scanner;
import java.text.NumberFormat;
public class StudentAverages
{
//----------------------------------------------------------------
// Reads a grade from the user and prints averages accordingly.
//----------------------------------------------------------------
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
NumberFormat fmt = new NumberFormat.getPercentInstance();
System.out.print("Enter the name of the first student: ");
String name1 = scan.nextLine();
int lab1, lab2, lab3, min = 0;
System.out.print("Enter the lab assignment grades of "+name1+": ");
lab1 = scan.nextInt();
lab2 = scan.nextInt();
lab3 = scan.nextInt();
System.out.print("Enter the project grades of "+name1+": ");
int proj1 = scan.nextInt();
int proj2 = scan.nextInt();
System.out.print("Enter the midterm grade of "+name1+ ": ");
double mid1 = scan.nextDouble();
System.out.print("Enter the final grade of "+name1+": ");
double fin1 = scan.nextDouble(); // fin used so as to not confuse with CONSTANT final
double avgLab1 = (lab1 + lab2 + lab3) / 3;
double avgProj1 = (proj1 + proj2) / 2;
double grade1 = (25 * avgLab1 + 40 * avgProj1 + 15 * mid1 + 20 * fin1) / 10000;
System.out.println();
System.out.println("Student's name " + name1);
System.out.println("Lab average for "+name1+": " + avgLab1);
System.out.println("Project average for "+name1+": " + avgProj1);
System.out.println("Total grade for "+name1+": " + fmt.format(grade1));
New at coding. Thanks for any help. I cross referenced the syntax from the book I'm using, and it checks out. I even checked previous code, and I did nothing differently, or at least nothing that I can see. So why would the compiler say it cannot find the symbol?
Instead of:
NumberFormat fmt = new NumberFormat.getPercentInstance();
it should be:
NumberFormat fmt = NumberFormat.getPercentInstance();
because, you want to call the static method getPercentInstance from class NumberFormat
.
Furthermore (as Pshemo said), the NumberFormat
class is abstract , so you cannot instantiate it with the new
keyword.
Remove the new from:
NumberFormat fmt = new NumberFormat.getPercentInstance();
You are calling a static method.
Remove new
keyword in line
NumberFormat fmt = new NumberFormat.getPercentInstance();
NumberFormat
is abstract
class which means you can't create instance of it.
You need instance of class which extends it, and such instance you can get for instance via getPercentInstance
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.