Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2012, 07:59 PM   PM User | #1
montecarlo2079
New to the CF scene

 
Join Date: Aug 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
montecarlo2079 is an unknown quantity at this point
Help with PRINTF

Im having trouble getting this code to display correctly.

this is what I get.

How can I get rid of the number before "Product Number"?
I know its having to reqrite my printf statement, but I have no idea how to rewrite it.

any help?

The total number of dvds is 5

0 Product Number = 0
DVD Name= Scream
# of Units = 5
Price Per Unit = $ 5.99
Total Inventory Value = $ 29.95

1 Product Number = 1
DVD Name= Scream 2
# of Units = 10
Price Per Unit = $ 6.99
Total Inventory Value = $ 69.90

2 Product Number = 2
DVD Name= Scream 3
# of Units = 10
Price Per Unit = $ 7.99
Total Inventory Value = $ 79.90

3 Product Number = 3
DVD Name= Scream 4
# of Units = 10
Price Per Unit = $ 8.99
Total Inventory Value = $ 89.90

4 Product Number = 4
DVD Name= Scream 5
# of Units = 10
Price Per Unit = $ 9.99
Total Inventory Value = $ 99.90

The total value of all the inventory is $369.55

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inventory1;

/**
 *
 * @author  */

 class Inventory1_2 {
    private DVD[] dvds;
    private int count;
    Inventory1_2(){
        dvds = new DVD[10];
        count = 0   ;         
    }
     public void add(DVD dvd){
         dvds[count]= dvd;
         ++count;
         sort();
     }
     public double totalvalue(){
         double value = 0;
         for (int i = 0; i < count;i++){
             value=value + dvds[i].totalvalue();
         }
         
         return value;
     }
     public void sort(){
         for (int index = 1; index < count; index++) { 
             DVD key = dvds[index]; 
             int position = index;
             
         while (position > 0 && key.getdvdname().compareTo(dvds [position-1].getdvdname())<0){
             dvds[position]=dvds[position-1];
             position--;
         }
         dvds[position]=key;
         }
     }
     public void display(){
         
         System.out.println("\nThe total number of dvds is "+ count);
         for (int i=0;i< count;i++)
             System.out.printf("\n%3d %s\n",i,dvds[i]);
         System.out.printf("\nThe total value of all the inventory is $%.2f\n\n",totalvalue());
     }
     
 }
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inventory1;

/**
 *
 * @author  */
public class Inventory2 {
    public static void main(String args []){
      Inventory1_2 invent = new Inventory1_2(); 
       
        DVD d1 = new DVD (0,"Scream",5,5.99);
        invent.add(d1);
        DVD d2 = new DVD (1,"Scream 2",10,6.99);
        invent.add(d2);
        DVD d3 = new DVD (2,"Scream 3",10,7.99);
        invent.add(d3);
        DVD d4 = new DVD (3,"Scream 4",10,8.99);
        invent.add(d4);
        DVD d5 = new DVD (4,"Scream 5",10,9.99);
        invent.add(d5);
        
        //display the inventory information one by one
    //System.out.println(d1);
    //System.out.println();
    //System.out.println(d2);
     //System.out.println();
    //System.out.println(d3);
     //System.out.println();
    //System.out.println(d4);
    // System.out.println();
    //System.out.println(d5);
       
  invent.display();
        
    
    }
}
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inventory1;

/**
 *
 * @author BurkhartR
 */
class DVD { 
    private int dvdprodnumber; 
    private String dvdname;
    private int dvdunits;
    private double dvdprice;
    
    public DVD(int prodnumber, String name, int units,double price){
        dvdprodnumber=prodnumber;
        dvdname=name;
        dvdunits=units;
        dvdprice=price;
    }//end of constructor
    
    //set dvd productnumber
    public void setDvdprodnumber(int prodnumber) { 
        dvdprodnumber=prodnumber;
	}//end method
    
    //return productnumber
    public int getdvdprodnumber(){
        return dvdprodnumber;
    }//end method
    
    //set the dvd name
    public void setdvdname(String name){
        dvdname=name;
    }//end method
    
    //get dvd name
    public String getdvdname(){
        return dvdname;   
    }//end method
    
    //set dvd price
    public void setdvdprice(double price)        {
        dvdprice=price;
    }//end method
    
    //get dvd price
    public double getdvdprice(){
        return dvdprice;
    }//end method
    
    public double totalvalue(){
        return dvdprice*dvdunits;
    }
            
 

	// This is how the display will look
    public String toString() {  

        return String.format("Product Number =%3d   \nDVD Name= %-20s   \n# of Units =%3d   \nPrice Per Unit =  $%6.2f   \nTotal Inventory Value =   $%7.2f",  
dvdprodnumber, dvdname, dvdunits, dvdprice, totalvalue());  

}

    
}
montecarlo2079 is offline   Reply With Quote
Old 08-22-2012, 09:57 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
This is the JavaSCRIPT forum.

Your question is about JAVA.

The two languages are about as similar as, say, Italian and Spanish. No, come to think of it they might not be that similar.

Try posting your question in the appropriate form.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-23-2012, 06:19 AM   PM User | #3
Brandnew
New Coder

 
Join Date: Aug 2012
Posts: 50
Thanks: 0
Thanked 11 Times in 11 Posts
Brandnew is an unknown quantity at this point
Thought i'd help as i'm on this learning curve on both languages, so take this very lightly as i'm not an expert but what i found to work:

Change your display method to the following:

Code:
    public void display(){
         
         System.out.println("\nThe total number of dvds is "+ count);
         for (int i=0;i< count;i++)
             System.out.print("\n"+dvds[i]+"\n");
         System.out.printf("\nThe total value of all the inventory is $%.2f\n\n",totalvalue());
     }
But also just keep in mind this forum is for JavaScript....not the same as JAVA. However i read that there is something called the "Google Web Toolkit (GWT), a set of opensource tools for web programming, that can convert a Java program into JavaScript.

Last edited by Brandnew; 08-23-2012 at 06:27 AM.. Reason: Adding info
Brandnew is offline   Reply With Quote
Users who have thanked Brandnew for this post:
montecarlo2079 (08-23-2012)
Old 08-23-2012, 01:09 PM   PM User | #4
montecarlo2079
New to the CF scene

 
Join Date: Aug 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
montecarlo2079 is an unknown quantity at this point
That did work!!! thank you.



Quote:
Originally Posted by Brandnew View Post
Thought i'd help as i'm on this learning curve on both languages, so take this very lightly as i'm not an expert but what i found to work:

Change your display method to the following:

Code:
    public void display(){
         
         System.out.println("\nThe total number of dvds is "+ count);
         for (int i=0;i< count;i++)
             System.out.print("\n"+dvds[i]+"\n");
         System.out.printf("\nThe total value of all the inventory is $%.2f\n\n",totalvalue());
     }
But also just keep in mind this forum is for JavaScript....not the same as JAVA. However i read that there is something called the "Google Web Toolkit (GWT), a set of opensource tools for web programming, that can convert a Java program into JavaScript.
montecarlo2079 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:56 PM.


Advertisement
Log in to turn off these ads.