output Array help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen3776
    New Member
    • Apr 2007
    • 5

    #1

    output Array help

    I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.doubl e !d. Can somebody tell me what I am doing wrong or if there is another way?

    Code:
    /*
     * Main.java
     *
     * Created on April 29, 2007, 6:57 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package inventory2;
    
    /**
     *
     * @author SandK
     */
    import java.util.*;
    import java.text.*;
    import java.lang.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.text.DecimalFormat;
    public class Main
    {  //declare variables
       private static String Choice;
       private static double movieTotal;
       private static double movieValue;
       private static double gameTotal;
       private static double gameValue;
       private static double appsTotal;
       private static double appsValue;
       private double movieNumber[][];
       private double gameNumber[][];
       private double appsNumber[][];
       private Product[] stores;
       double itemNumber[][];
       static double value;
       static double grandTotal;
       
       
       public static void main(String[] args)
       {//set values for multiple array
           //assign empty set to clear final variable
           double itemNumber[][] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
            //assign values to Movie array  
           double movieNumber[][] = { {5,10,29.95}, {6,15,24.95}, {7,20,14.95},
                                      {8,20,15.95} };
           //assign values to Game array
           double gameNumber[][] = { {9,10,229.95}, {10,5,199.95}, {11,4,4999.95},
                                     {12,20,699.95} };
           //assign value to Apps array
           double appsNumber[][] = { {1,10,19.95}, {2,25,25.95}, {3,20,20.95},
                                     {4,15,19.95} }; 
            
           Main inventory = new Main(); 
              {
           Scanner input = new Scanner (System.in);
           //Query user for inventory to view
           System.out.println("Would you like to view the movies, games, or apps inventory?");
           String userChoice = input.nextLine();
           Choice = userChoice;
           //Load values into array for movies selection
           if( userChoice.equalsIgnoreCase("movies"))
           { 
             
             //add new DVD's to inventory  
             //Create subclass objects for DVD subclass including style
             inventory.addProduct(new DVD("White Noise", 1, 10, 19.95, "Action"));
             inventory.addProduct(new DVD("Jeff Foxworthy", 2, 25, 25.95, "Comedy"));
             inventory.addProduct(new DVD("Cars", 3, 20, 20.95, "Comedy"));
             inventory.addProduct(new DVD("Monster-In-Law", 4, 15, 19.95, "Comedy"));
            //assign multiple variable movieNumber[][] to 
            //itemNumber[][] for array output         
            for (int disc = 0; disc < itemNumber.length; disc++)
            {
                for(int column = 0; column < itemNumber[disc].length; column++)
                {
                 itemNumber = movieNumber;
                }
             }
    
           }
           else if (userChoice.equalsIgnoreCase("games")) 
           { 
            //add new games to inventory
            //Create subclass objects for Game subclass without style
            inventory.addProduct(new Games("Command and Conquer", 5, 10, 29.95 ));
            inventory.addProduct(new Games("Delta Force II", 6, 15, 24.95));
            inventory.addProduct(new Games("Roller Coaster Tycoon", 7, 20, 14.95));
            inventory.addProduct(new Games("Half-Life", 8, 20, 15.95));
            //assign multiple variable gameNumber[][] to 
            //itemNumber[][] for array output
            for (int disc = 0; disc < itemNumber.length; disc++)
            {
                for(int column = 0; column < itemNumber[disc].length; column++)
                {
                 itemNumber = gameNumber;
                }
             }
            
    
           }
           else if (userChoice.equalsIgnoreCase("apps")) 
           { 
            //add new games to inventory
            //Create subclass objects for Game subclass with style
            inventory.addProduct(new Apps("Wiindows Vista 32 bit", 9, 10, 229.95,
                                          "Operating System" ));
            inventory.addProduct(new Apps("Windows Vista 64 bit", 10, 5, 199.95,
                                          "Operating System"));
            inventory.addProduct(new Apps("Autocad 2007", 11, 5, 4999.95, "Drafting"));
            inventory.addProduct(new Apps("Microsoft Office 2007", 12, 20, 699.95,
                                          "Business"));
            //assign multiple variable appsNumber[][] to 
            //itemNumber[][] for array output
            for (int disc = 0; disc < itemNumber.length; disc++)
            {
                for(int column = 0; column < itemNumber[disc].length; column++)
                {
                 itemNumber = appsNumber;
                }
             }
           }
      //Determine individual totals for arrays to compute grand total
            //Compute movie total from array for part of grand total
            for (int disc = 0; disc < movieNumber.length; disc++)
            {
                for(int column = 0; column < movieNumber[disc].length; column++)
                {
                 movieValue = movieNumber[disc][1] * itemNumber[disc][2];
                 movieTotal += movieValue;
                }
            }      
            //Compute game total from array for part of grand total
            for (int disc = 0; disc < gameNumber.length; disc++)
            {
                for(int column = 0; column < gameNumber[disc].length; column++)
                {
                 gameValue = gameNumber[disc][1] * itemNumber[disc][2];  
                 gameTotal += gameValue;
                }
            }
            //Compute apps total from array for part of grand total    
            for (int disc = 0; disc < appsNumber.length; disc++)
            {
                for(int column = 0; column < appsNumber[disc].length; column++)
                {
                 appsValue = appsNumber[disc][1] * itemNumber[disc][2];
                 appsTotal += appsValue;
                }
             }      
        grandTotal = movieTotal + gameTotal + appsTotal;
      }
          
          System.out.println("Inventory of: ");
          System.out.println(Choice);
          System.out.println();
    
    
          System.out.println();
          //Call to superclass methods
          inventory.displayInv();
          inventory.nameSort();
          System.out.println();
          inventory.displayInv();
          
          double total = inventory.totalInvCalc();
          System.out.println("Total Value is: $" + total);
          System.out.println();
          System.out.println("The grand stock inventory value is: $");
          System.out.println(grandTotal);
         
          
          
    
             outputArray(itemNumber);
    
       }
       
           public static void outputArray(double itemNumber[][])
        {
             //Print Column headings and set spacing
            System.out.printf("%5s%6s%5s%12s\n", "Disc", "Qty", "Cost", "Inv Value");
            //Print values from array and set spacing between values
            //set row (aka disc) and column to zero before starting
            for (int disc = 0; disc < itemNumber.length; disc++)
            {
                for(int column = 0; column < itemNumber[disc].length; column++)
    
                //print information from array            
                System.out.printf("%5d", itemNumber[disc][column]); 
                 value = 0;
                //determine next value
                value = itemNumber[disc][1] * itemNumber[disc][2];
                grandTotal += value;
                //print next value
                System.out.printf("%8d",value);
                //Return to start new line
                //Clear values before determing next value            
                System.out.println();
                
             }
           System.out.print("The total inventory value is: $");
           System.out.println(grandTotal);
           System.out.println();
     }
       // method to sort inventory by name
       public void nameSort()
       {
          for (int i = 1; i < stores.length; i++)
          {
             int j;
             Product val = stores[i];
             for (j = i - 1; j > -1; j--)
             {
                Product temp = stores[j];
                if (temp.compareTo(val) <= 0)
                {
                   break;
                }
                stores[j + 1] = temp;
             }
             stores[j + 1] = val;
          }
       }
    
       //method to create a String representation of array of class products
       public String toString()
       {
          String s = "";
          for (Product p : stores)
          {
             s = s + p.toString();
             s = s + "\n\n";
          }
          return s;
       }
    
       //method to increase array size to add products
       public void addProduct(Product p1)
       {
          if (stores == null)
          {
             stores = new Product[0];
          }
          //Copy all products into p first
          Product[] p = stores; 
          //create bigger array
          Product[] temp = new Product[p.length + 1]; 
          //add the new product at the last position
          for (int i = 0; i < p.length; i++)
          {
             temp[i] = p[i];
          }
          temp[(temp.length - 1)] = p1; 
          stores = temp;
       }
    
       //method to calculate Total Inventory Value
       public double totalInvCalc()
       {
          double total = 0.0;
          for (int i = 0; i < stores.length; i++)
          {
             total = total + stores[i].inventoryValue();
          }
          return total;
       }
       //method to display inventory
       public void displayInv()
       {
          //call toString method
          System.out.println(toString()); 
       }
    
    
    }
    
    
    //Setup Products subclass to Main and superclass to DVD's
    
    
    
    }
  • Stephen3776
    New Member
    • Apr 2007
    • 5

    #2
    output Array help part2

    This part is not the problem, but here is the rest of the code:

    Code:
    class Product extends Main implements Comparable
    {  //Declare class variables to store item information
       private String title;   
       private int item;     
       private double stockOnHand;   
       private double cost;     
       //Declare product constructor for use by subclasses and clear variables
       public Product()
       {
          title = "";
          item = 0;
          stockOnHand = 0;
          cost = 0.0;
       }
       //four argument constructor
       public Product(String title, int item, double stockOnHand, double cost)
       {
          this.title = title;
          this.item = item;
          this.stockOnHand = stockOnHand;
          this.cost = cost;
       }
       //Setup set and get methods for class Products
       public void setTitle(String title)
       {
          this.title = title;
       }
    
       public String getTitle()
       {
          return title;
       }
    
       public void setItem(int item)
       {
          this.item = item;
       }
    
       public int getItem()
       {
          return item;
       }
    
       public void setstockOnHand(double quantity)
       {
          stockOnHand = quantity;
       }
    
       public double getstockOnHand()
       {
          return stockOnHand;
       }
    
       public void setItemcost(double cost)
       {
          this.cost = cost;
       }
    
       public double getItemcost()
       {
          return cost;
       }
    
       public double inventoryValue()
       {
          return cost * stockOnHand;
       }
       //implement compare method
       public int compareTo(Object o)
       {
          Product p = null;
          try
          {
             p = (Product) o;
          }
          catch (ClassCastException cE)
          {
             cE.printStackTrace();
          }
          return title.compareTo(p.getTitle());
       }
       //Setup method to output string overriding string in superclass 
       //inventory2.Main using toString
       public String toString()
       {
          return "Disc Title: " + title + "\nItem #: " + item + "\ncost: $" + cost + "\nQuantity: "
          + stockOnHand + "\nValue: $" + inventoryValue();
       }
    }
    //Setup class DVD subclass to Products
    class DVD extends Product
    {
       //Declare variables for DVD subclass
       private String style;
       private double restockFee;
    
       //Five argument constructor
       public DVD(String title, int item, double stockOnHand, double cost, String style)
       {
          super(title,item,  stockOnHand, cost);
          this.style = style;
          this.restockFee = 0.05;
       }
    
       //returns the value of the inventory, plus the restocking fee
       public double getInventoryValue()
       {
         return super.getItemcost() + restockFee;
       }
       //Return string with override of string of superclass inventory2.Product
       //using toString
       public String toString()
       {
          //Return string with style of DVD added
          return super.toString() + "\nstyle      \t" + style;
       }
    
    }
    //Setup Games as subclass of Products
    class Games extends Product
    {
       //Declare variables for subclass Games
        private double restockFee;
    
       //Four argument constructor for subclass Games
       public Games(String title, int item, double stockOnHand, double cost)
       { 
          super(title, item, stockOnHand, cost);
          
          this.restockFee = 0.05;
       }
    
       //returns the value of the inventory, plus the restocking fee
       public double getInventoryValue()
       {
        return super.getItemcost() + restockFee;
       }
       //Return string with override of string of superclass inventory2.Product
       //using toString
       public String toString()
       {
         return super.toString();
       }
    }
    class Apps extends Product
    {
       //Declare variables for DVD subclass
       private String type;
       private double restockFee;
    
       //Five argument constructor
       public Apps(String title, int item, double stockOnHand, double cost, String type)
       {
          super(title,item,  stockOnHand, cost);
          this.type = type;
          this.restockFee = 0.05;
       }
    
       //returns the value of the inventory, plus the restocking fee
       public double getInventoryValue()
       {
         return super.getItemcost() + restockFee;
       }
       //Return string with override of string of superclass inventory2.Product
       //using toString
       public String toString()
       {
          //Return string with style of DVD added
          return super.toString() + "\ntype      \t" + type;
       }

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Stephen3776
      I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.doubl e !d. Can somebody tell me what I am doing wrong or if there is another way?

      Code:
      /*
      * Main.java
      *
      * Created on April 29, 2007, 6:57 PM
      *
      * To change this template, choose Tools | Template Manager
      * and open the template in the editor.
      */
       
      package inventory2;
       
      /**
      *
      * @author SandK
      */
      import java.util.*;
      import java.text.*;
      import java.lang.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.*;
      import java.text.DecimalFormat;
      public class Main
      { //declare variables
      private static String Choice;
      private static double movieTotal;
      private static double movieValue;
      private static double gameTotal;
      private static double gameValue;
      private static double appsTotal;
      private static double appsValue;
      private double movieNumber[][];
      private double gameNumber[][];
      private double appsNumber[][];
      private Product[] stores;
      double itemNumber[][];
      static double value;
      static double grandTotal;
       
       
      public static void main(String[] args)
      {//set values for multiple array
      //assign empty set to clear final variable
      double itemNumber[][] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
      //assign values to Movie array 
      double movieNumber[][] = { {5,10,29.95}, {6,15,24.95}, {7,20,14.95},
      {8,20,15.95} };
      //assign values to Game array
      double gameNumber[][] = { {9,10,229.95}, {10,5,199.95}, {11,4,4999.95},
      {12,20,699.95} };
      //assign value to Apps array
      double appsNumber[][] = { {1,10,19.95}, {2,25,25.95}, {3,20,20.95},
      {4,15,19.95} }; 
       
      Main inventory = new Main(); 
      {
      Scanner input = new Scanner (System.in);
      //Query user for inventory to view
      System.out.println("Would you like to view the movies, games, or apps inventory?");
      String userChoice = input.nextLine();
      Choice = userChoice;
      //Load values into array for movies selection
      if( userChoice.equalsIgnoreCase("movies"))
      { 
       
      //add new DVD's to inventory 
      //Create subclass objects for DVD subclass including style
      inventory.addProduct(new DVD("White Noise", 1, 10, 19.95, "Action"));
      inventory.addProduct(new DVD("Jeff Foxworthy", 2, 25, 25.95, "Comedy"));
      inventory.addProduct(new DVD("Cars", 3, 20, 20.95, "Comedy"));
      inventory.addProduct(new DVD("Monster-In-Law", 4, 15, 19.95, "Comedy"));
      //assign multiple variable movieNumber[][] to 
      //itemNumber[][] for array output 
      for (int disc = 0; disc < itemNumber.length; disc++)
      {
      for(int column = 0; column < itemNumber[disc].length; column++)
      {
      itemNumber = movieNumber;
      }
      }
       
      }
      else if (userChoice.equalsIgnoreCase("games")) 
      { 
      //add new games to inventory
      //Create subclass objects for Game subclass without style
      inventory.addProduct(new Games("Command and Conquer", 5, 10, 29.95 ));
      inventory.addProduct(new Games("Delta Force II", 6, 15, 24.95));
      inventory.addProduct(new Games("Roller Coaster Tycoon", 7, 20, 14.95));
      inventory.addProduct(new Games("Half-Life", 8, 20, 15.95));
      //assign multiple variable gameNumber[][] to 
      //itemNumber[][] for array output
      for (int disc = 0; disc < itemNumber.length; disc++)
      {
      for(int column = 0; column < itemNumber[disc].length; column++)
      {
      itemNumber = gameNumber;
      }
      }
       
       
      }
      else if (userChoice.equalsIgnoreCase("apps")) 
      { 
      //add new games to inventory
      //Create subclass objects for Game subclass with style
      inventory.addProduct(new Apps("Wiindows Vista 32 bit", 9, 10, 229.95,
      "Operating System" ));
      inventory.addProduct(new Apps("Windows Vista 64 bit", 10, 5, 199.95,
      "Operating System"));
      inventory.addProduct(new Apps("Autocad 2007", 11, 5, 4999.95, "Drafting"));
      inventory.addProduct(new Apps("Microsoft Office 2007", 12, 20, 699.95,
      "Business"));
      //assign multiple variable appsNumber[][] to 
      //itemNumber[][] for array output
      for (int disc = 0; disc < itemNumber.length; disc++)
      {
      for(int column = 0; column < itemNumber[disc].length; column++)
      {
      itemNumber = appsNumber;
      }
      }
      }
      //Determine individual totals for arrays to compute grand total
      //Compute movie total from array for part of grand total
      for (int disc = 0; disc < movieNumber.length; disc++)
      {
      for(int column = 0; column < movieNumber[disc].length; column++)
      {
      movieValue = movieNumber[disc][1] * itemNumber[disc][2];
      movieTotal += movieValue;
      }
      } 
      //Compute game total from array for part of grand total
      for (int disc = 0; disc < gameNumber.length; disc++)
      {
      for(int column = 0; column < gameNumber[disc].length; column++)
      {
      gameValue = gameNumber[disc][1] * itemNumber[disc][2]; 
      gameTotal += gameValue;
      }
      }
      //Compute apps total from array for part of grand total 
      for (int disc = 0; disc < appsNumber.length; disc++)
      {
      for(int column = 0; column < appsNumber[disc].length; column++)
      {
      appsValue = appsNumber[disc][1] * itemNumber[disc][2];
      appsTotal += appsValue;
      }
      } 
      grandTotal = movieTotal + gameTotal + appsTotal;
      }
       
      System.out.println("Inventory of: ");
      System.out.println(Choice);
      System.out.println();
       
       
      System.out.println();
      //Call to superclass methods
      inventory.displayInv();
      inventory.nameSort();
      System.out.println();
      inventory.displayInv();
       
      double total = inventory.totalInvCalc();
      System.out.println("Total Value is: $" + total);
      System.out.println();
      System.out.println("The grand stock inventory value is: $");
      System.out.println(grandTotal);
       
       
       
       
      outputArray(itemNumber);
       
      }
       
      public static void outputArray(double itemNumber[][])
      {
      //Print Column headings and set spacing
      System.out.printf("%5s%6s%5s%12s\n", "Disc", "Qty", "Cost", "Inv Value");
      //Print values from array and set spacing between values
      //set row (aka disc) and column to zero before starting
      for (int disc = 0; disc < itemNumber.length; disc++)
      {
      for(int column = 0; column < itemNumber[disc].length; column++)
       
      //print information from array 
      System.out.printf("%5d", itemNumber[disc][column]); 
      value = 0;
      //determine next value
      value = itemNumber[disc][1] * itemNumber[disc][2];
      grandTotal += value;
      //print next value
      System.out.printf("%8d",value);
      //Return to start new line
      //Clear values before determing next value 
      System.out.println();
       
      }
      System.out.print("The total inventory value is: $");
      System.out.println(grandTotal);
      System.out.println();
      }
      // method to sort inventory by name
      public void nameSort()
      {
      for (int i = 1; i < stores.length; i++)
      {
      int j;
      Product val = stores[i];
      for (j = i - 1; j > -1; j--)
      {
      Product temp = stores[j];
      if (temp.compareTo(val) <= 0)
      {
      break;
      }
      stores[j + 1] = temp;
      }
      stores[j + 1] = val;
      }
      }
       
      //method to create a String representation of array of class products
      public String toString()
      {
      String s = "";
      for (Product p : stores)
      {
      s = s + p.toString();
      s = s + "\n\n";
      }
      return s;
      }
       
      //method to increase array size to add products
      public void addProduct(Product p1)
      {
      if (stores == null)
      {
      stores = new Product[0];
      }
      //Copy all products into p first
      Product[] p = stores; 
      //create bigger array
      Product[] temp = new Product[p.length + 1]; 
      //add the new product at the last position
      for (int i = 0; i < p.length; i++)
      {
      temp[i] = p[i];
      }
      temp[(temp.length - 1)] = p1; 
      stores = temp;
      }
       
      //method to calculate Total Inventory Value
      public double totalInvCalc()
      {
      double total = 0.0;
      for (int i = 0; i < stores.length; i++)
      {
      total = total + stores[i].inventoryValue();
      }
      return total;
      }
      //method to display inventory
      public void displayInv()
      {
      //call toString method
      System.out.println(toString()); 
      }
       
       
      }
       
       
      //Setup Products subclass to Main and superclass to DVD's
       
       
       
      }
      At which line are you getting the error?

      Comment

      • Stephen3776
        New Member
        • Apr 2007
        • 5

        #4
        The line I am getting the error on is:

        Code:
        outputArray(itemNumber);

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Stephen3776
          The line I am getting the error on is:

          Code:
          outputArray(itemNumber);
          Post the full error message please.

          Comment

          • Stephen3776
            New Member
            • Apr 2007
            • 5

            #6
            Thanks for the help, glad it was so simple

            Comment

            Working...