473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Program won't store integer from seperate file

As the title of the question states, my program is not able to store an integer from a seperate text file provided. For instance, instead of printing the desired output:

"$45.00 17 2222 Chuck Taylor All Star"

It prints:

"$45.00 17 0 Chuck Taylor All Star"

So even though I provided the price (double), quantity (integer) and name (string), my program can store all of it except for the blasted ID number (integer). If anyone is willing to guide me towards the light on this, I would greatly appreciate it. My code is below, and the last bit of data at the very end is what "productin.txt" contains.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.text.NumberFormat;
  3.  
  4. public class Product
  5. {
  6.    private String name;
  7.     private double price;
  8.     private int idNum;
  9.     private int quantity;
  10.  
  11.     public Product (int id, String title, double cost, int total) 
  12.     {
  13.         id = idNum;
  14.         name = title;
  15.         price = cost;
  16.         quantity = total;
  17.     }
  18.  
  19.    //-----------------------------------------------------------------
  20.    //  Returns the product's ID number.
  21.    //-----------------------------------------------------------------
  22.  
  23.     public int getID()
  24.     {
  25.        return idNum;
  26.     }
  27.  
  28.     //-----------------------------------------------------------------
  29.    //  Returns the product's name.
  30.    //-----------------------------------------------------------------
  31.  
  32.     public String getName()
  33.     {
  34.        return name;
  35.     }
  36.  
  37.     //-----------------------------------------------------------------
  38.    //  Returns the product's price.
  39.    //-----------------------------------------------------------------
  40.  
  41.     public double getPrice()
  42.     {
  43.        return price;
  44.     }
  45.  
  46.     //-----------------------------------------------------------------
  47.    //  Adds units to the quantity in inventory.
  48.    //-----------------------------------------------------------------
  49.  
  50.     public void addQuantity(int units)
  51.    {
  52.       quantity += units;
  53.    }
  54.  
  55.     //-----------------------------------------------------------------
  56.    //  Returns the quantity in inventory.
  57.    //-----------------------------------------------------------------
  58.  
  59.  
  60.     public int getQuantity()
  61.     {
  62.        return quantity;
  63.     }
  64.  
  65.     //-----------------------------------------------------------------
  66.    //  Subtracts units to the quantity in inventory.
  67.    //-----------------------------------------------------------------
  68.  
  69.     public void subQuantity(int units)
  70.    {
  71.       quantity -= units;
  72.    }
  73.  
  74.     //-----------------------------------------------------------------
  75.    //  Returns a printablle version of the product object.
  76.    //-----------------------------------------------------------------
  77.  
  78.    public String toString()
  79.    {
  80.       NumberFormat fmt = NumberFormat.getCurrencyInstance();
  81.  
  82.       String description;
  83.  
  84.       description = fmt.format(price) + "\t" + quantity + "\t";
  85.       description += idNum + "\t" + name;
  86.  
  87.       return description;  
  88.    }
  89. }
  90.  
Expand|Select|Wrap|Line Numbers
  1. import java.text.NumberFormat;
  2.  
  3. public class ProductCollection
  4. {
  5.    private Product[] collection;
  6.    private int count;
  7.     private double totalCost;
  8.  
  9.    //-----------------------------------------------------------------
  10.    //  Constructor: Creates an initially empty collection.
  11.    //-----------------------------------------------------------------
  12.    public ProductCollection ()
  13.    {
  14.       collection = new Product[100];
  15.       count = 0;
  16.         totalCost = 0.0;
  17.    }
  18.  
  19.    //-----------------------------------------------------------------
  20.    //  Adds a product to the collection.
  21.    //-----------------------------------------------------------------
  22.  
  23.    public void addProduct (int prodID, String prodName, double unitPrice, int prodQty)
  24.    {
  25.       if (count == collection.length)
  26.          increaseSize();
  27.  
  28.       int index = findProduct(prodID);
  29.  
  30.       if (index == -1)
  31.       {
  32.           collection[count] = new Product (prodID, prodName, unitPrice, prodQty);
  33.           totalCost += unitPrice;
  34.           count++;
  35.       }
  36.       else
  37.           System.out.println("Product was not added, a product having the ID number " + prodID 
  38.                             + " was found.\n");
  39.    }
  40.  
  41.    //-----------------------------------------------------------------
  42.    //  Increases the size of the product to the collection.
  43.    //-----------------------------------------------------------------
  44.  
  45.     private void increaseSize ()
  46.    {
  47.       Product[] temp = new Product[collection.length * 2];
  48.  
  49.       for (int prod = 0; prod < collection.length; prod++)
  50.          temp[prod] = collection[prod];
  51.  
  52.       collection = temp;
  53.     }
  54.  
  55.    //-----------------------------------------------------------------
  56.    //  Finds product from collection.
  57.    //-----------------------------------------------------------------
  58.  
  59.     public int findProduct(int prodID)
  60.    {
  61.        int index = -1;
  62.       for (int i = 0; i < count; i++)
  63.       if (collection[i].getID() == prodID)
  64.           index = i;
  65.       return index;
  66.     }
  67.  
  68.    //-----------------------------------------------------------------
  69.    //  Changes the price of the product from the collection.
  70.    //-----------------------------------------------------------------
  71.  
  72.     public void changePrice(int prodID, double newPrice)
  73.     {
  74.       int index = findProduct(prodID);
  75.       if (index > -1)
  76.          collection[index].setPrice(newPrice);
  77.       else
  78.          System.out.println("The product having the ID number " + prodID 
  79.                             + " was not found.\n");    
  80.     }
  81.  
  82.    //-----------------------------------------------------------------
  83.    //  Deletes product from the collection.
  84.    //-----------------------------------------------------------------
  85.  
  86.     public void deleteProduct(int prodID)
  87.     {
  88.        int index = findProduct(prodID);
  89.  
  90.         if (index > -1)
  91.       {
  92.             for (int i = index; i < count; i++)
  93.            {
  94.                collection[i] = collection[i+1];
  95.                count--;
  96.             }
  97.         }
  98.         else System.out.println("The product having the ID number " + prodID 
  99.                             + " was not found.\n");
  100.     }
  101.  
  102.    //-----------------------------------------------------------------
  103.    //  Buys product units to the collection.
  104.    //-----------------------------------------------------------------
  105.  
  106.     public void addQty(int prodID, int prodQty)
  107.    {
  108.       int index = findProduct(prodID);
  109.       if (index > -1)
  110.          collection[index].addQuantity(prodQty);
  111.       else
  112.          System.out.println("The product having the ID number " + prodID 
  113.                             + " was not found.\n");
  114.    }
  115.  
  116.    //-----------------------------------------------------------------
  117.    //  Sells product units from the collection.
  118.    //-----------------------------------------------------------------
  119.  
  120.     public void subQty(int prodID, int prodQty)
  121.    {
  122.       int index = findProduct(prodID);
  123.       if (index > -1)
  124.          collection[index].subQuantity(prodQty);
  125.       else
  126.          System.out.println("The product having the ID number " + prodID 
  127.                             + " was not found.\n");
  128.    }
  129.  
  130.    //-----------------------------------------------------------------
  131.    //  Displays product from the collection.
  132.    //-----------------------------------------------------------------
  133.  
  134.     public void displayProduct(int prodID)
  135.     {
  136.       int index = findProduct(prodID);
  137.       if (index > -1)
  138.          System.out.println(collection[index]);
  139.       else
  140.          System.out.println("The product having the ID number " + prodID 
  141.                             + " was not found.\n");    
  142.     }
  143.  
  144.    //-----------------------------------------------------------------
  145.    //  Returns a report describing the Product collection.
  146.    //-----------------------------------------------------------------
  147.  
  148.        public String createOutputFile()
  149.    {
  150.       String data = "";
  151.       for (int i = 0; i < count; i++)
  152.       {
  153.           data += collection[i].getID() + ",";
  154.           data += collection[i].getName() + ",";
  155.           data += collection[i].getPrice() + ",";
  156.           data += collection[i].getQuantity() + ",\n";
  157.       }
  158.       return data;
  159.    }
  160.  
  161.    public String toString()
  162.    {
  163.       NumberFormat fmt = NumberFormat.getCurrencyInstance();
  164.  
  165.       String report = "\n--------------------------------------------------\n\n";
  166.       report += "Collection of Converse\n\n";
  167.  
  168.       report += "Number of products: " + count + "\n";
  169.       report += "Total cost: " + fmt.format(totalCost) + "\n";
  170.       report += "Average cost: " + fmt.format(totalCost/count);
  171.  
  172.       report += "\n\nShoe List:\n\n";
  173.  
  174.       for (int prod = 0; prod < count; prod++)
  175.          report += collection[prod].toString() + "\n";
  176.  
  177.       return report;
  178.    }
  179.  
  180. }
  181.  
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class ProgTwo
  5. {
  6.    public static void main (String[] args) throws FileNotFoundException, IOException
  7.    {
  8.       Scanner scan = new Scanner(new File("productin.txt"));
  9.         Scanner userInput = new Scanner(System.in);
  10.       ProductCollection data = new ProductCollection ();
  11.       String name = null;
  12.       double price = 0.0;
  13.       int idNum = 0, quantity = 0, input = 0;
  14.  
  15.         while (scan.hasNextLine())
  16.       {
  17.          scan.useDelimiter(",");
  18.             idNum = scan.nextInt();
  19.          name = scan.next();
  20.          price = scan.nextDouble();
  21.          quantity = scan.nextInt();
  22.          scan.nextLine();
  23.          data.addProduct (idNum, name, price, quantity);
  24.       }
  25.  
  26.         System.out.println("\nThis program asks you to select a number from");
  27.        System.out.println("the choices listed below of the popular brand");
  28.          System.out.println("Converse. Select shoes from Chuck Taylors to");
  29.        System.out.println("Jack Purcell are available. In addition, you");
  30.          System.out.println("are encouraged to add or delete shoe brands of");
  31.          System.out.println("your own, as well as a number of other options.\n");
  32.          do
  33.          { 
  34.               System.out.println("1. Display one product");
  35.                System.out.println("2. Display all products");
  36.             System.out.println("3. Add a product");
  37.             System.out.println("4. Delete a product");
  38.             System.out.println("5. Buy product units");
  39.             System.out.println("6. Sell product units");
  40.             System.out.println("7. Change price of product");
  41.             System.out.println("8. Exit");
  42.             System.out.print("\nEnter your choice: ");
  43.             input = userInput.nextInt();
  44.  
  45.               switch (input)
  46.             {
  47.                    case 1:  System.out.print("\nEnter the ID number of the product to display: " );
  48.                         idNum = userInput.nextInt();
  49.                         data.displayProduct(idNum);
  50.                                 break; 
  51.                     case 2:  System.out.println (data);
  52.                              break; 
  53.                case 3:  System.out.print("\nEnter the ID number: ");                                
  54.                         idNum =userInput.nextInt();
  55.                         System.out.print("\nEnter the name: " );
  56.                         name = userInput.nextLine();
  57.                         System.out.print("\nEnter the price: " );
  58.                         price = userInput.nextDouble();
  59.                         System.out.print("\nEnter the quantity: ");
  60.                         quantity = userInput.nextInt();
  61.                         data.addProduct(idNum, name, price, quantity);
  62.                         System.out.println(data);                   
  63.                         break;
  64.                case 4:  System.out.print("\nEnter the ID number of the product to delete: ");
  65.                         idNum = userInput.nextInt();
  66.                         data.deleteProduct(idNum);
  67.                         System.out.println(data);
  68.                                 break;
  69.                     case 5:  System.out.print("\nEnter ID number of product to modify: ");
  70.                         idNum = userInput.nextInt();
  71.                         System.out.print("\nHow many product units to buy? ");
  72.                         quantity = userInput.nextInt();
  73.                         userInput.nextLine();
  74.                         data.addQty(idNum, quantity);
  75.                         System.out.println(data);
  76.                                 break;
  77.                     case 6:  System.out.print("\nEnter ID number of product to modify: ");
  78.                         idNum = userInput.nextInt();
  79.                         System.out.print("\nHow many product units to sell? ");
  80.                         quantity = userInput.nextInt();
  81.                         userInput.nextLine();
  82.                         data.subQty(idNum, quantity);
  83.                         System.out.println(data);
  84.                                 break;
  85.                case 7:  System.out.print("\nEnter the ID number of product to modify: ");
  86.                         idNum = userInput.nextInt();
  87.                         System.out.print("\nWhat is the new price? ");
  88.                         price = userInput.nextDouble();
  89.                         userInput.nextLine();
  90.                         data.changePrice(idNum, price);
  91.                         System.out.println(data);
  92.  
  93.             }
  94.          } while (input != 8);
  95.  
  96.       FileWriter fw = new FileWriter("productout.txt");
  97.       BufferedWriter bw = new BufferedWriter(fw);
  98.       PrintWriter pw = new PrintWriter(bw);
  99.  
  100.       pw.println(data.createOutputFile());
  101.       pw.close();
  102.     }
  103. }
  104.  
Expand|Select|Wrap|Line Numbers
  1. 1111,Chuck Taylor All Star Slim,50.00,22,
  2. 2222,Chuck Taylor All Star,45.00,17,
  3. 3333,Chuck Taylor All Star Trompe L'oeil,49.99,10,
  4. 4444,Chuck Taylor All Star Dble Tongue,39.99,15,
  5. 5555,Kids 0-3 years Chuck Taylors All Star,19.99,23,
  6. 6666,Jack Purcell Garment Dye,60.00,12,
  7. 7777,Jack Purcell African Canvas,65.00,18,
  8. 8888,Jack Purcell Low Profile Slip,70.00,29,
  9. 9999,Kids 4-7 years Jack Purcell,34.99,10,
  10.  
Apr 18 '10 #1
1 1969
pbrockway2
151 Expert 100+
Your output is showing the id number. That number is zero.

Do you expect it to be non-zero? If so, go back to the code that assigns the id number - at the start of the Product constructor - and check that code to see why the idNum field remains zero.
Apr 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: yongwei | last post by:
I have a user control that used to work fine. But after I moved the project to another folder, one of the user control won't generate a .resouce file after rebuild, and the form contains the user...
3
by: Karen Grube | last post by:
Hi! Each week, we receive a two-page PDF file from UPS along with a separate flat file (a CSV) The PDF file contains the overview of our weekly invoice and the CSV contains the details of each...
0
by: | last post by:
Greetings all I can't create access databases because I keep getting cant find system.mdw file. Access won't create sysem.mdw file automaticlly or manually when I go to the security section and...
3
by: Paminu | last post by:
I have made a lot of helping functions that I would like to have in a seperate file. I have tried putting them in a file "functions.c". I then have the file "myprogram.c" that contains some...
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.