473,772 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program won't store integer from seperate file

11 New Member
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 1992
pbrockway2
151 Recognized Expert New Member
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
1435
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 control won't work, I get a run time exception: Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly. make sure "UserControl.resources" was correctly embedded or linked into assembly...
3
13388
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 shipment. I download the file from UPS and then use DTS to import the data into SQL. At that point, I have a Crystal report that prints what looks like a regular UPS invoice. The only problem is that I wind up with having to print two...
0
433
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 Try Whya
3
1342
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 structs and other primary functions. In my make file I then have: myprogram: myprogram.c functions.c gcc myprogram.c -o myprogram
5
3305
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 namespace std; int read(string filename, int last_pos) {
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.