473,395 Members | 1,574 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,395 software developers and data experts.

Inventory program only reads one line

Hello. I have a program that is supposed to read a .txt file, save the data in an array, then print out that data. I have a program that compiles, but when I run it, it does not print the first line of the .txt file, prints the second line, then does not print the third line, but stays in the run and freezes. Here's the .txt file which is named "inventory.txt"...

Basketball 2 20.00
Baseball 3 5.00
Soccerball 5 20.00

...and the output should be...

Peter's Sport Store Inventory List

Item Units Price Total Price
Basketball 2 20.00 40.00
Baseball 3 5.00 15.00
Soccerball 5 20.00 100.00

Here is the driver program that I've come up with so far:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.*;
  3. import java.text.*;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Inventory {
  7.  
  8. public static void main(String[] args) throws IOException {
  9.  
  10.        BufferedReader stdin = new BufferedReader(new                   InputStreamReader (System.in));
  11.         final int MAX = 500;
  12.         InventoryItem[] items = new InventoryItem[MAX];
  13.         StringTokenizer tokenizer;
  14.         String line, name, file = "inventory.txt";
  15.         int units, count = 0;
  16.         double price;
  17.  
  18.         try {
  19.  
  20.             FileReader fr = new FileReader(file);
  21.  
  22. BufferedReader inFile = new BufferedReader(fr);
  23.  
  24. line = inFile.readLine();
  25.  
  26.             while ((line = inFile.readLine()) != null) {
  27.                 tokenizer = new StringTokenizer(line);
  28.  
  29.                 while (tokenizer.hasMoreElements()) {
  30.                     name = tokenizer.nextToken();
  31.  
  32.      try {
  33.     units = Integer.parseInt(tokenizer.nextToken());
  34.  
  35.     price = Double.parseDouble(tokenizer.nextToken());
  36.  
  37.     items[count++] = new InventoryItem(name, units, price); }
  38.  
  39.     catch (NumberFormatException exception) {
  40.  
  41.     System.out.println("Error in input. Line ignored:");
  42.     System.out.println(line);
  43. }
  44.     line = inFile.readLine();
  45. }
  46. }
  47.     inFile.close();
  48.     for (int scan = 0; scan < count; scan++)
  49.     System.out.println(items[scan]);
  50.     } 
  51.  
  52.     catch (FileNotFoundException exception){
  53.     System.out.println("The file " + file + " was not found.");
  54.  
  55.      catch (IOException exception){
  56.  
  57.      System.out.println(exception);
  58. }
  59.         stdin.read();
  60. }
  61. }     
  62.  
I'm really confused as to why the program only reads the second line, then refuses to loop and read the third one. Thanks for any help!
Apr 20 '15 #1
2 1716
rajujrk
107 100+
Hi,

I have just modified your code by commented some lines, there is no big changes


Inventory.java
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.io.*;
  4. import javax.swing.*;
  5. import java.text.*;
  6. import java.util.StringTokenizer;
  7.  
  8. public class Inventory {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.  
  12.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  13.                 System.in));
  14.         final int MAX = 500;
  15.         InventoryItem[] items = new InventoryItem[MAX];
  16.         StringTokenizer tokenizer;
  17.         String line, name, file = "C:\\Raju_Work\\inventory.txt";
  18.         int units, count = 0;
  19.         double price;
  20.  
  21.         try {
  22.  
  23.             FileReader fr = new FileReader(new File(file));
  24.  
  25.             BufferedReader inFile = new BufferedReader(fr);
  26.  
  27.             //line = inFile.readLine();
  28.  
  29.             while ((line = inFile.readLine()) != null) {
  30.                 tokenizer = new StringTokenizer(line);
  31.  
  32.  
  33.                 while (tokenizer.hasMoreElements()) {
  34.                     name = tokenizer.nextToken();
  35.  
  36.                     try {
  37.                         units = Integer.parseInt(tokenizer.nextToken());
  38.  
  39.                         price = Double.parseDouble(tokenizer.nextToken());
  40.  
  41.                         items[count++] = new InventoryItem(name, units, price);
  42.  
  43.                     }
  44.  
  45.                     catch (NumberFormatException exception) {
  46.  
  47.                         System.out.println("Error in input. Line ignored:");
  48.                         System.out.println(line);
  49.                     }
  50.                     //line = inFile.readLine();
  51.                 }
  52.             }
  53.             inFile.close();
  54.             for (int scan = 0; scan < count; scan++)
  55.                 System.out.println(items[scan]);
  56.         }
  57.  
  58.         catch (FileNotFoundException exception) {
  59.             System.out.println("The file " + file + " was not found.");
  60.         }
  61.  
  62.         catch (IOException exception) {
  63.  
  64.             System.out.println(exception);
  65.         }
  66.         stdin.read();
  67.     }
  68. }
  69.  
And InventoryItem.java


Expand|Select|Wrap|Line Numbers
  1. public class InventoryItem {
  2.  
  3.     private String name;
  4.     private int unit;
  5.     private Double price;
  6.  
  7.     private Double total;
  8.  
  9.     public InventoryItem(String name, int unit, Double price) {
  10.         // TODO Auto-generated constructor stub
  11.  
  12.         this.name = name;
  13.         this.unit = unit;
  14.         this.price = price;
  15.  
  16.         this.total = price * unit;
  17.     }
  18.  
  19.     @Override
  20.     public String toString() {
  21.         // TODO Auto-generated method stub
  22.         String strRetval = name + " " + unit + " " + price + " " + total;
  23.         return strRetval;
  24.     }
  25.  
  26. }
  27.  
  28.  

OUT PUT FOrmat:

Basketball 2 20.0 40.0
Baseball 3 5.0 15.0
Soccerball 5 20.0 100.0

Thanks
Rajkumar
May 11 '15 #2
chaarmann
785 Expert 512MB
for every iteration, code in line 26 and also code in line 44 both read a line. So you always read 2 lines per iteration, but do not do anything with the second line.
Just remove line 44 and the code runs a sexpected.
May 18 '15 #3

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

Similar topics

109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
3
by: cblank | last post by:
I need some help if someone could help me. I know everyone is asking for help in java. But for some reason I'm the same as everyone else when it comes to programming in java. I have an inventory...
5
by: cblank | last post by:
I'm having some trouble with my inventory program. Its due tom and my teacher is not wanting to help. He keeps giving me a soluction that is not related to my code. I have everything working except...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
1
by: jcato77 | last post by:
I need help with a class project I'm working on, Below is my assignment and the code I have currently created. Assignment: Modify the Inventory Program by creating a subclass of the product class...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.