Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,412 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,452 people online right now. Registration is fast and FREE... Join Now!




Code Problem

 
Reply to this topicStart new topic

Code Problem, Finished the code but need some more help...

J.A.LrnQuic
14 Oct, 2008 - 07:25 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 3


My Contributions
This is my Assignment:

A mail-order house sells five different products whose retail prices are as follows: product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49; and product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:

a) Product number

cool.gif Quantity sold for one day

Your program should use a switch structure to help determine the retail price for each product. It should calculate and display the total retail value of each products sold last week. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

This is my PROBLEM:
I spent a lot of time browsing the web and reading how to do such things as a switch statement and a sentinel-controlled loop and now that I have the code completed it runs and as it goes through the info I have a double product that ends with a zero and I think it is messing up my code...take a look...I would like to know how to get the value to stay at for example $28.82 where I'm getting $28.820000000000004

also am I missing anything?
Thank you

CODE
package ch5;

import java.util.Scanner;

public class Ex517 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner inp = new Scanner(System.in);
        
         int product,quantity;
        double total=0.00;
        
        System.out.print("Enter product #(1-5)(0 to stop): ");
        product=inp.nextInt();
        
while(product !=0){
    System.out.print("Enter quantity: ");
    quantity=inp.nextInt();
        switch( product ){
        case 1:total+=quantity*2.98;
        break;
        case 2:total+=quantity*4.50;
        break;
        case 3:total+=quantity*9.98;
        break;
        case 4:total+=quantity*4.49;
        break;
        case 5:total+=quantity*6.87;
        break;
        default:System.out.println("Invalid Product Number");
        System.out.println("Product Number Does not Exist");
        
        if(product<0 && product>=6){
        System.out.print("Enter product #(1-5)(0 to stop): ");
        product=inp.nextInt();
        System.out.print("Enter quantity: ");
        quantity=inp.nextInt();}
        break;
        }
        System.out.print("Enter product #(1-5)(0 to stop): ");
        product=inp.nextInt();    
        }
        {
        
            System.out.println( "The total retail value was: "+total );
    
}
    }
    {
        
    }
    {
    }}


This is the answer I get once Ive entered the product number and the quantity sold:
Enter product #(1-5)(0 to stop): 1
Enter quantity: 1
Enter product #(1-5)(0 to stop): 2
Enter quantity: 1
Enter product #(1-5)(0 to stop): 3
Enter quantity: 1
Enter product #(1-5)(0 to stop): 4
Enter quantity: 1
Enter product #(1-5)(0 to stop): 5
Enter quantity: 1
Enter product #(1-5)(0 to stop): 6
Enter quantity: 1
Invalid Product Number
Product Number Does not Exist
Enter product #(1-5)(0 to stop): 0
The total retail value was: $28.820000000000004

This post has been edited by J.A.LrnQuic: 14 Oct, 2008 - 07:52 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Code Problem
14 Oct, 2008 - 08:14 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
You can use

Old ways of doing things:

DecimalFormat df = new DecimalFormat("#.##");
String twoDecimal = df.format(total);
System.out.println("Total: " + twoDecimal);

or use, since JRE 1.5

System.out.printf("Total: %10.2f\n", total); // 10 digit 2 digits after decimal
User is offlineProfile CardPM
+Quote Post

J.A.LrnQuic
RE: Code Problem
15 Oct, 2008 - 05:43 AM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 3


My Contributions
QUOTE(pbl @ 14 Oct, 2008 - 09:14 PM) *

You can use

Old ways of doing things:

DecimalFormat df = new DecimalFormat("#.##");
String twoDecimal = df.format(total);
System.out.println("Total: " + twoDecimal);

or use, since JRE 1.5

System.out.printf("Total: %10.2f\n", total); // 10 digit 2 digits after decimal


Thank you Im gonna try this out tonight and get back with results...
User is offlineProfile CardPM
+Quote Post

bbq
RE: Code Problem
15 Oct, 2008 - 06:30 AM
Post #4

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
java
System.out.println("Some Double Value : " + String.format("%.2f", total);


should work
User is offlineProfile CardPM
+Quote Post

J.A.LrnQuic
RE: Code Problem
15 Oct, 2008 - 02:02 PM
Post #5

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 3


My Contributions
QUOTE(bbq @ 15 Oct, 2008 - 07:30 AM) *

java
System.out.println("Some Double Value : " + String.format("%.2f", total);


should work


Thank you. put it in and now im getting $28.82 instead of $28.80000000000004
User is offlineProfile CardPM
+Quote Post

bbq
RE: Code Problem
16 Oct, 2008 - 12:18 AM
Post #6

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
Awesome glad it helped biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:27PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month