Help with DecimalFormat Class

I am having probs getting DecimalFormat Class to work

Page 1 of 1

2 Replies - 685 Views - Last Post: 19 February 2009 - 09:47 PM Rate Topic: -----

#1 silverpetal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 09

Help with DecimalFormat Class

Post icon  Posted 19 February 2009 - 07:29 PM

hi all! I am having probs getting the DecimalFormat Class to work in my code. It is supposed to allow the user enter inputs in decimals (2 decimal places). Also the average needs to reflect this. I think maybe I need to add something to the system.out.println? Not sure, please keep in mind this is for a beginning course. I am only on chapter 3 hehe. Thanks a bunch!
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main 
{
	public static void main(String[] args) {
	 System.out.println("\n\n\n");
	 System.out.println("Hello, welcome to Naomi's Java world!");
	 System.out.println("\t\t........................\t\t");

			int score, totalScore, scoreA, scoreB, scoreC, average;
			char grade;

			System.out.println("Please enter a grade between 0 and 100:");
			Scanner keyboard = new Scanner(System.in);
			DecimalFormat formattingObject = new DecimalFormat("000.00");
			scoreA = keyboard.nextInt();

			if (scoreA >= 90)
			{grade = 'A';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is an " + grade);}
			else if (scoreA >= 80)
				{grade = 'B';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreA >= 70)
			{grade = 'C';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreA >= 60)
				{grade = 'D';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreA>= 50)
				 {grade = 'F';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}

			System.out.println("Please enter a grade between 0 and 100:");
			keyboard = new Scanner(System.in);
			scoreB = keyboard.nextInt();

			if (scoreB >= 90)
			{grade = 'A';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is an" + grade);}
			else if (scoreB >= 80)
				{grade = 'B';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreB >= 70)
			{grade = 'C';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreB >= 60)
				{grade = 'D';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreB >= 50)
				 {grade = 'F';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			
			System.out.println("Please enter a grade between 0 and 100:");
			keyboard = new Scanner(System.in);
			scoreC = keyboard.nextInt();
			totalScore = scoreA + scoreB + scoreC;
			average = totalScore/3;

			if (scoreC >= 90)
			{grade = 'A';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is an" + grade);}
			else if (scoreC >= 80)
				{grade = 'B';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreC >= 70)
			{grade = 'C';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreC >= 60)
				{grade = 'D';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreC>= 50)
				 {grade = 'F';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}

				System.out.println ("Total score entered " + totalScore);
				System.out.println ("The total average is " + average);





	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with DecimalFormat Class

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with DecimalFormat Class

Posted 19 February 2009 - 07:31 PM

import java.util.Scanner;
import java.text.DecimalFormat;

public class Changes
{
	public static void main(String[] args) {
	 System.out.println("\n\n\n");
	 System.out.println("Hello, welcome to Naomi's Java world!");
	 System.out.println("\t\t........................\t\t");

			double score,scoreA, scoreB, scoreC;
			char grade;
		double totalScore, average;
			System.out.println("Please enter a grade between 0 and 100:");
			Scanner keyboard = new Scanner(System.in);
			DecimalFormat formattingObject = new DecimalFormat("000.00");
			scoreA = keyboard.nextDouble();

			if (scoreA >= 90)
			{grade = 'A';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is an " + grade);}
			else if (scoreA >= 80)
				{grade = 'B';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreA >= 70)
			{grade = 'C';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreA >= 60)
				{grade = 'D';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreA>= 50)
				 {grade = 'F';
				System.out.println ("Thank you and you have entered " + scoreA);
				System.out.println ("The letter grade is " + grade);}

			System.out.println("Please enter a grade between 0 and 100:");
			keyboard = new Scanner(System.in);
			scoreB = keyboard.nextDouble();

			if (scoreB >= 90)
			{grade = 'A';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is an" + grade);}
			else if (scoreB >= 80)
				{grade = 'B';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreB >= 70)
			{grade = 'C';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreB >= 60)
				{grade = 'D';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreB >= 50)
				 {grade = 'F';
				System.out.println ("Thank you and you have entered " + scoreB);
				System.out.println ("The letter grade is " + grade);}
		   
			System.out.println("Please enter a grade between 0 and 100:");
			keyboard = new Scanner(System.in);
			scoreC = keyboard.nextDouble();
			totalScore = scoreA + scoreB + scoreC;
			average = totalScore/3;

			if (scoreC >= 90)
			{grade = 'A';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is an" + grade);}
			else if (scoreC >= 80)
				{grade = 'B';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreC >= 70)
			{grade = 'C';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreC >= 60)
				{grade = 'D';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}
			else if (scoreC>= 50)
				 {grade = 'F';
				System.out.println ("Thank you and you have entered " + scoreC);
				System.out.println ("The letter grade is " + grade);}

				System.out.println ("Total score entered " + formattingObject.format(totalScore));
				System.out.println ("The total average is " + formattingObject.format(average));
	}
}


by the way your grades needed to be doubles

This post has been edited by mostyfriedman: 19 February 2009 - 07:42 PM

Was This Post Helpful? 1
  • +
  • -

#3 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: Help with DecimalFormat Class

Posted 19 February 2009 - 09:47 PM

Hi, You could also use a loop in this program. It would condense your code quite a bit and work the same. I can't remember what chapter I learned loops in last semester but i do remember it being early on. I hope this is helpful in some way. This code uses a While loop.
import java.util.Scanner;
import java.text.DecimalFormat;

public class Changes
{
	public static void main(String[] args) {
	 System.out.println("\n\n\n");
	 System.out.println("Hello, welcome to Naomi's Java world!");
	 System.out.println("\t\t........................\t\t");
			int loopNum = 1;
			double score;
			char grade;
			double totalScore = 0, average;
			
			System.out.println("Please enter a grade between 0 and 100:");
			Scanner keyboard = new Scanner(System.in); // create scanner object
			DecimalFormat formattingObject = new DecimalFormat("###.##"); // defines decimal format
			score = keyboard.nextDouble();
		while(loopNum < 3)// this loop will run until loopNum reaches 3
			{
				if (score >= 90)
				{grade = 'A';
					System.out.println ("Thank you and you have entered " + score);
					System.out.println ("The letter grade is an " + grade);}
				else if (score >= 80)
					{grade = 'B';
					System.out.println ("Thank you and you have entered " + score);
					System.out.println ("The letter grade is " + grade);}
				else if (score >= 70)
				{grade = 'C';
					System.out.println ("Thank you and you have entered " + score);
					System.out.println ("The letter grade is " + grade);}
				else if (score >= 60)
					{grade = 'D';
					System.out.println ("Thank you and you have entered " + score);
					System.out.println ("The letter grade is " + grade);}
				else if (score>= 50)
					 {grade = 'F';
					System.out.println ("Thank you and you have entered " + score);
					System.out.println ("The letter grade is " + grade);}
					
				++loopNum;
			
				System.out.println("Please enter a grade between 0 and 100:");
				keyboard = new Scanner(System.in);
				score = keyboard.nextDouble();
				totalScore = totalScore + score; // holds total of scores entered for later use
		}
			average = totalScore / 3;
			System.out.println ("Total score entered " + formattingObject.format(totalScore));
			System.out.println ("The total average is " + formattingObject.format(average));
	}
}


Hope this is helpful
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1