Results 1 to 7 of 7
  1. #1
    Ross92 is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Need program to print in decimal format

    I need this program to print out the gpa down to 2 decimal places and I can't figure out how to do it. It keeps saying it can't find decimal format and I'm not sure how to define it If you could help me out I'd really appreciate it

    Java Code:
    import java.util.Scanner;
    
    public class GPACalculator {
        public static void main(String[] args) {
            double creditHours = 0;
            double gradePoints = 0;
            
            System.out.println("Enter number of classes");
            Scanner scanner = new Scanner(System.in);
            double numClass = scanner.nextInt();
            
            for(double i = 1; i <= numClass; i++){
                System.out.println("Enter credit hours for class "+ i);
                double aCreditHour = scanner.nextInt();
                creditHours += aCreditHour;
                System.out.println("Enter grade for class "+ i);
                String letter = scanner.next();
                if(letter.equals("A")){
                    gradePoints = gradePoints + (4 * aCreditHour);
                } else if (letter.equals("B")){
                    gradePoints = gradePoints + (3 * aCreditHour);
                } else if (letter.equals("C")){
                    gradePoints = gradePoints + (2 * aCreditHour);
                } else if (letter.equals("D")){
                    gradePoints = gradePoints + (1 * aCreditHour);
                } else if (letter.equals("F")){
                    gradePoints = gradePoints + (0 * aCreditHour);
                }
            }
            decimalFormat df = new decimalFormat("#0.00");
            System.out.println("Your GPA is: " + decimalFormat.format(gradePoints / creditHours));
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Need program to print in decimal format

    keeps saying it can't find decimal forma
    Java is case sensitive. d is different than D. Check the spelling.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ross92 is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need program to print in decimal format

    Quote Originally Posted by Norm View Post
    Java is case sensitive. d is different than D. Check the spelling.
    I've changed the spelling to as many different ways as I could think of and it still just tells me that it cannot find symbol DecimalFormat. I can't really figure out what it's asking for.

  4. #4
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Need program to print in decimal format

    For this to work:
    You first need to import the DecimalFormat class. (note: remember Java is case sensitive).

    You should also have a look at line 32.
    Java Code:
    System.out.println("Your GPA is: " + DecimalFormat.df(gradePoints / creditHours));
    DeicmalFormat.df is not the correct way to use the class after you defined it as df.

    Hint: Have a look at how you used your Scanner after you defined it as scanner.

    Give it a try :)

  5. #5
    Ross92 is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need program to print in decimal format

    Alright, so I've got it down to that last line after I figured out how to define DecimalFormat, but I'm not sure what I'm supposed to do with that final line it looks like this System.out.println("Your GPA is: " + DecimalFormat.format(gradePoints / creditHours));

    and I get a message saying non-static method format(double) cannot be referenced from a static context. what exactly does that mean?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Need program to print in decimal format

    You need an import statement for the DecimalFormat class so the compiler can find it definition. Just like the one you have for the Scanner class.

    non-static method format(double) cannot be referenced from a static context.
    You need a reference to the DecimalFormat class to call the format() method. df on line 30 is a reference.
    Last edited by Norm; 10-25-2014 at 01:05 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Ross92 is offline Member
    Join Date
    Oct 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need program to print in decimal format

    I figured it out, thank you guys for your help

Similar Threads

  1. Decimal.Format error
    By roofninja in forum New To Java
    Replies: 4
    Last Post: 07-09-2012, 11:24 PM
  2. How to Format a double value to 2 decimal places
    By nandhinianand in forum New To Java
    Replies: 3
    Last Post: 04-30-2012, 01:26 AM
  3. How do you format to two decimal places with println()?
    By EscSequenceAlpha in forum New To Java
    Replies: 4
    Last Post: 04-05-2012, 08:27 PM
  4. Can't get the decimal format to format correctly...
    By Valerie1067 in forum New To Java
    Replies: 5
    Last Post: 03-25-2012, 04:15 AM
  5. Replies: 7
    Last Post: 09-14-2011, 03:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •