# Help with DecimalFormat Class

Page 1 of 1

## 2 Replies - 1066 Views - Last Post: 19 February 2009 - 09:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=87797&amp;s=dbdc34fbf429bfa6b1152427a62c54a7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 silverpetal

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

# Help with DecimalFormat Class

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;

Scanner keyboard = new Scanner(System.in);
DecimalFormat formattingObject = new DecimalFormat("000.00");
scoreA = keyboard.nextInt();

if (scoreA >= 90)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA >= 80)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA >= 70)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA >= 60)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA>= 50)
System.out.println ("Thank you and you have entered " + scoreA);

keyboard = new Scanner(System.in);
scoreB = keyboard.nextInt();

if (scoreB >= 90)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 80)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 70)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 60)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 50)
System.out.println ("Thank you and you have entered " + scoreB);

keyboard = new Scanner(System.in);
scoreC = keyboard.nextInt();
totalScore = scoreA + scoreB + scoreC;
average = totalScore/3;

if (scoreC >= 90)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC >= 80)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC >= 70)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC >= 60)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC>= 50)
System.out.println ("Thank you and you have entered " + scoreC);

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

• The Algorithmi

Reputation: 729
• 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;
double totalScore, average;
Scanner keyboard = new Scanner(System.in);
DecimalFormat formattingObject = new DecimalFormat("000.00");
scoreA = keyboard.nextDouble();

if (scoreA >= 90)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA >= 80)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA >= 70)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA >= 60)
System.out.println ("Thank you and you have entered " + scoreA);
else if (scoreA>= 50)
System.out.println ("Thank you and you have entered " + scoreA);

keyboard = new Scanner(System.in);
scoreB = keyboard.nextDouble();

if (scoreB >= 90)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 80)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 70)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 60)
System.out.println ("Thank you and you have entered " + scoreB);
else if (scoreB >= 50)
System.out.println ("Thank you and you have entered " + scoreB);

keyboard = new Scanner(System.in);
scoreC = keyboard.nextDouble();
totalScore = scoreA + scoreB + scoreC;
average = totalScore/3;

if (scoreC >= 90)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC >= 80)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC >= 70)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC >= 60)
System.out.println ("Thank you and you have entered " + scoreC);
else if (scoreC>= 50)
System.out.println ("Thank you and you have entered " + scoreC);

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

```

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

### #3 Lucrosen

Reputation: 5
• 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;
double totalScore = 0, average;

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)
System.out.println ("Thank you and you have entered " + score);
else if (score >= 80)
System.out.println ("Thank you and you have entered " + score);
else if (score >= 70)
System.out.println ("Thank you and you have entered " + score);
else if (score >= 60)
System.out.println ("Thank you and you have entered " + score);
else if (score>= 50)
System.out.println ("Thank you and you have entered " + score);

++loopNum;

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));
}
}

```