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

Thread: Not sure how to use DecimalFormat class

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not sure how to use DecimalFormat class

    Does it need an output statement or something? If so where do i put it?
    package gradebook;
     
     
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class reportcard {
    public static void main (String[] args){
    //A new school in the area has hired you to build a grade
    //report software for them.  The school wants the software
    //interaction to be as follows:
     
    // Name    string
    // Test1   double
    // Test2   double
    // Test3   double
    // Test4   double
    // Test5   double
    // Avg   double   
    // Avg= (Test1+Test2+Test3+Test4+Test5)/5.0;
    //The software should also display the letter grade based on the grade scale:
    //  A: 90 - 100   
    //  B: 80 - 89
    //  C: 70 - 79
    //  D: 60 - 69
    //  F: 0 - 59
    //The program shall end if the average for the final grade is below zero (0)
    //or higher than 100.
     
    		//Intro
    		System.out.print("Welcome to the gradebook.");
     
     
    		String name;
    		double test1;
    		double test2;
    		double test3;
    		double test4;
    		double test5;
    		double average;
    		String input;
     
     
    		//Students Full Name  
    	      name = JOptionPane.showInputDialog("Enter students " +
    	                                              "name.");
     
     
    	    //Decimal formatter  
    	      DecimalFormat dF =
    	    		  new DecimalFormat("#0.00");
     
     
     
    	    // Get test grade in numerical values
     
    	      input = JOptionPane.showInputDialog("Enter grade for test1");
    	      test1 = Double.parseDouble(input);
     
    	      input = JOptionPane.showInputDialog("Enter grade for test2");
    	      test2 = Double.parseDouble(input);
     
    	      input = JOptionPane.showInputDialog("Enter grade for test3");
    	      test3 = Double.parseDouble(input);
     
    	      input = JOptionPane.showInputDialog("Enter grade for test4");
    	      test4 = Double.parseDouble(input);
     
    	      input = JOptionPane.showInputDialog("Enter grade for test5");
    	      test5 = Double.parseDouble(input);
     
    	    //Get average score
     
    	      average = (test1 + test2 + test3 + test4 + test5) / 5.0;
     
     
     
     
    	   // Output the average score.
    	      JOptionPane.showMessageDialog(null, 
    	                                "The average is for " + average );
     
     
     
    	   // Display the grade.
    	      if (average < 60)
    	      {
    	         JOptionPane.showMessageDialog(null, "Letter Grade = F.");
    	      }
    	      else
    	      {
    	         if (average < 70)
    	         {
    	            JOptionPane.showMessageDialog(null, "Letter Grade = D.");
    	         }
    	         else
    	         {
    	            if (average < 80)
    	            {
    	               JOptionPane.showMessageDialog(null, "Letter Grade = C.");
    	            }
    	            else
    	            {
    	               if (average < 90)
    	               {
    	                  JOptionPane.showMessageDialog(null, "Letter Grade = B.");
    	               }
    	               else
    	               {
    	                  JOptionPane.showMessageDialog(null, "Letter Grade = A.");
    	               }
    	            }
    	         }
     
    	    //final message
    	         JOptionPane.showMessageDialog(null, 
                         "The average for\n " + name + " is " + average );
     
     
    	      System.exit(0);
     
     
     
    }}}
    Last edited by jprg12; October 7th, 2014 at 03:48 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Not sure how to use DecimalFormat class

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
    Does it need an output statement or something? If so where do i put it?
    To do what? Please describe what you're trying to fix, what you need help with.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to use DecimalFormat class

    Well the instructions are: the software shall calculate and display the average of the test scores as the final grade. This score is to be displayed to one decimal place using the DecimalFormat class.

    After reading my book, I know I should be using this.
    //Decimal formatter  
    	      DecimalFormat dF =
    	    		  new DecimalFormat("#0.00");
    On this line I have an error that says "The value of the local variable df is not being used." So I can only assume that there is more to it than what I have written.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Not sure how to use DecimalFormat class

    That's not an error.

    Review the DecimalFormat tutorial that starts in the middle of this page.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure how to use DecimalFormat class

    Thanks! I didn't understand at first but I got it eventually. I was able to make my program better.

Similar Threads

  1. Need help with DecimalFormat class
    By FlyingFingers in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2014, 04:50 AM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. DecimalFormat error when compiled
    By roofninja in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 11th, 2012, 10:48 PM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. MessageDialog working with DecimalFormat?
    By Plural in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2010, 04:07 AM