Domanda

EDIT:

  1. you guys are really fast at answering. I love it.
  2. I can not believe I missed the fact that I tried instantiating the NumberFormat class meanwhile when I previously used it, I didn't do that. I stared at this code for so long too. Looks like I have a lot to learn when it comes to paying close attention to detail.

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top