Reading a file

I can't get Java to read a text document!

Page 1 of 1

7 Replies - 1293 Views - Last Post: 28 March 2010 - 07:39 PM Rate Topic: -----

#1 Matt_Bellamy_47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-March 10

Reading a file

Posted 28 March 2010 - 03:32 PM

What I'm trying to do is to take values from "intenvory.txt" and then using the 'Item' class find the PIV (mean value of all entries, ex. '69,7' is 7 units of 69 = 483) of all the values.
I can't get Java to read the Word document and could use some direction as how to store them into 'Item' and from there work with the numbers.

Text document
-----------------------------
8
CartridgeB,25.69,7
Battery ,89.00, 5
nkJet , 99.00 , 2
MonitorA , 169.00,5
MonitorB ,319.00,8
Printer , 699.00,5
Chair , 300.00,4
Desk , 500.00,2

Item class
-----------------------------
public class Item {

private String name ;
private double unitPrice ;
private int quantity ;

public Item ( String record) {
init (record.trim()) ;
}
public String getName () { return name; }
public double getUnitPrice () { return unitPrice; }
public int getQuantity () { return quantity; }

public void init (String record) {
String[] fields = record.split(",") ;
name = fields[0].trim() ;
unitPrice = Double.parseDouble (fields[1].trim());
quantity = Integer.parseInt (fields[2].trim());
}
}


Main
------------------------------
import javax.swing.JOptionPane;
import java.io.IOException ;
import java.io.*;

public class InventoryMan
{

public static void main(String[] args) throws IOException
{
int []values = new int [20];
FileReader inputFile = new FileReader("inventory.txt");
String filename = (inputFile);

readFile (values, filename);
printArray(values);
}
private static void readFile (int [] values, String filename)
throws IOException
{
String str;
FileReader inputFile = new FileReader (filename);
BufferedReader inputBuffer = new BufferedReader (inputFile);

str = inputBuffer.readLine(); // first read
int i = 0;

while (str != null)
{
values [i] = Integer.parseInt(str);
str = inputBuffer.readLine();
i++;
}
}
private static void printArray(int [] values)
{
for (int i = 0; i < values.length; i++)
System.out.println("" + i + " " + values[i]);
}

}

** I've been trying a lot of different things in order to check if it's working so it may look a bit strange but I'm quite lost at this point.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a file

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading a file

Posted 28 March 2010 - 03:40 PM

You misspelled "inventory" (as you have it in the program) as "intenvory" at the top of message. If you have the files misnamed, then of course Java isn't reading your file.

Check file spelling and make sure it matches in the program. Also make sure the file is in the same directory as the rest of your program or else you have to call that directory manually.

This post has been edited by zim1985: 28 March 2010 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 Matt_Bellamy_47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-March 10

Re: Reading a file

Posted 28 March 2010 - 03:56 PM

I made a spelling mistake when discribing my code but not actually in it.
Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading a file

Posted 28 March 2010 - 04:44 PM

Please put your code in the [code] your code here [/code] brackets in your OP, That way we can see what is going on easier.
Was This Post Helpful? 0
  • +
  • -

#5 Matt_Bellamy_47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-March 10

Re: Reading a file

Posted 28 March 2010 - 04:54 PM

import javax.swing.JOptionPane;
import java.io.IOException ;
import java.io.*;

public class InventoryMan
{

    public static void main(String[] args) throws IOException
    {
        int []values = new int [8];
        
                String filename = ("inventory.txt");

                
                readFile (values, filename);
          
    }
    private static void readFile (int [] values, String filename)
        throws IOException
    {
        String str;
        FileReader inputFile = new FileReader (filename);
        BufferedReader inputBuffer = new BufferedReader (inputFile);
        
        str = inputBuffer.readLine();   // first read
        int i = 0;
        
        while (str != null)
        {
                values [i] = Integer.parseInt(str);
                str = inputBuffer.readLine();
                i++;
        }
    }
 
    
}


This post has been edited by Matt_Bellamy_47: 28 March 2010 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Reading a file

Posted 28 March 2010 - 05:31 PM

well, set your file as a csv file, in that pattern:
name,price,amount
now, for each line you read, use the split(String regex) method.
this method tokenizes your String Object by a regex you provide, and returns a String array holding the tokens.
as:
str = inputBuffer.readLine(); 
String[] data = str.split("\\,");


now, for example that line of the file:
CartridgeB,25.69,7
is actually:
String[] data = {"CartridgeB", "25.69", "7"};

if you know that the first cell represents item's name, leave it as String.
String name = data[0];

the second cell represent a price as double, so parse it to double:
double price = Double.parseDouble(data[1]);

the third cell is amount, an int value:
int amount = Integer.parseInt(data[2]);


now all you have to do, is create a new Item by passing those variables as parameters:
Item newItem = new Item(name, price, amount);


The way you do it is also fine (i just demonstrated a different approach).
the problem is the way you split the String.
use
recoed.split("\\,");
//instead of
record.split(",");

also, zim1985 just asked you to follw the rules of the forum.
That is NOT a reason to downvote him.

This post has been edited by japanir: 28 March 2010 - 05:31 PM

Was This Post Helpful? 1
  • +
  • -

#7 Matt_Bellamy_47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-March 10

Re: Reading a file

Posted 28 March 2010 - 07:35 PM

Thanks a lot for the help ! :)
I'm getting closer but I do need to it the way that I was and your way seems a bit complicated for my level.
I didn't realize that 'downvoting' had such a negative effect, my apologizes to zim1985.
Was This Post Helpful? 0
  • +
  • -

#8 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading a file

Posted 28 March 2010 - 07:39 PM

He was just suggesting that because you have erratically separated pieces of information, that you are going to have to find another way to pick up each piece of information. If it was just delimited by spaces, then it would be much easier, but since you are using commas, you have to tell the InputStream to pick up information starting at a new, user specified character.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1