Problem reading data and storing it

Discussion in 'Programming General' started by Gohan, Feb 8, 2011.

Problem reading data and storing it
  1. Unread #1 - Feb 8, 2011 at 1:39 AM
  2. Gohan
    Joined:
    Mar 16, 2007
    Posts:
    23,695
    Referrals:
    16
    Sythe Gold:
    248
    Discord Unique ID:
    100075291572998144
    Spam Forum Participant Rust Player

    Gohan Legend
    Retired Sectional Moderator Cracker Head $25 USD Donor Prince Yobabo

    Problem reading data and storing it

    Code:
    public static void readFile() throws IOException{
    		BufferedReader read = new BufferedReader(new FileReader("src/receipts.txt"));
    		Item[] items = new Item[12];
    		
    		String line;
    		int lineCount = 0;
    		while ((line = read.readLine()) != null) {
    			lineCount++;
    		}
    		
    		System.out.println(lineCount);
    		
    		for(int i = 0; i<lineCount ; i++){
    	    	System.out.println(i);
    			String code = read.readLine();
    	    	String description = read.readLine();
    	    	double price = read.read();
    	    	int stock = read.read();
    	    	items[i] = new Item(code,description,price,stock);
    	    	
    	    }
    }
    
    output:
    60
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
    	at Read.readFile(Read.java:27)
    	at Functionality.main(Functionality.java:16)
    Code:
    GR0001
    Kleenex Toilet Rolls 18 pk
    6.00
    200
    
    TF0001
    Heinz Baked Beans 600g
    0.90
    500
    
    GR0002
    Morning Fresh Dishwashing Liquid 1 ltr
    3.25
    150
    
    TF0002
    John West Tuna 450g
    2.75
    35
    
    PF0001
    Vetta Spaghetti No 9. 500g
    1.50
    120
    
    TF0003
    Heinz Big Eat Meatballs 485g
    1.20
    20
    
    PF0002
    Vetta Egg Fettuccini 500g
    2.50
    100
    
    TF0004
    Heinz Peas &#8216;n Corn 485g
    0.90
    210
    
    PF0003
    Vetta Bolognese Sauce 575g
    3.00
    160
    
    GR0003
    Morning Fresh Dishwasher Tablets 6pk
    5.50
    90
    
    GR0004
    Chux Kitchen Wipes 10pk
    1.50
    70
    
    PF0004
    Vetta Carbonara Sauce 575g
    3.00
    30
    



    ,,,,,,
     
  3. Unread #2 - Feb 8, 2011 at 9:21 AM
  4. Fate1
    Joined:
    Apr 21, 2005
    Posts:
    773
    Referrals:
    2
    Sythe Gold:
    5

    Fate1 Apprentice
    Banned

    Problem reading data and storing it

    From looking at your code you declared an Item array of size 12. When you run your second for loop it
    Code:
    items[i] = new Item(code,description,price,stock)
    items is whatever i is on until it is greated than linecount. If linecount is greater than 12 it will throw an exception. And it looks like that your .txt file has more than 12 lines.

    But i don't know if thats right I'm in my second semester with Java lol.
     
  5. Unread #3 - Feb 8, 2011 at 10:34 AM
  6. Xeox
    Joined:
    Jun 17, 2010
    Posts:
    357
    Referrals:
    0
    Sythe Gold:
    0

    Xeox Forum Addict
    Banned

    Problem reading data and storing it

    I think you ment to write:

    while ((line == read.readLine()) != null)

    don't declare something with an = in a while, for, if, or do parameters.

    also I would suggest using an Array List (import java.util.ArrayList) so you don't have an IndexOutOfBounds exception. Array Lists are great because you can keep adding to them by using the .add method, and because plain arrays are very primative.

    I'm not completely sure what you are trying to use this for, but this is a very simple program I made that reads a text file and makes a copy of the same one. It might be of some use to you.You need to specify the name of the input text file, and name the output text file whatever you want.

    Code:
    /**
     * Write a description of class LineNumberer here.
     * 
     * @author (Mike Perugini) 
     * @version (11.3)
     */
    import java.io.*;
    import java.util.Scanner;
    
    public class CopyFile
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            FileReader reader = new FileReader("input.txt");
            
            PrintWriter out = new PrintWriter("output.txt");
            
            Scanner in = new Scanner(reader);
            
            while (in.hasNextLine())
            {
                String line = in.nextLine();
                out.println(line);
            }
            out.close();
        }
    }
    
    This is an enhanced version of that program that simply reads a text file and adds line numbers to it. You need to specify the name of the input text file, and name the output text file whatever you want.

    Code:
    /**
     * Write a description of class LineNumberer here.
     * 
     * @author (Mike Perugini) 
     * @version (11_tutorial_1)
     */
    import java.io.FileReader;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
    public class LineNumberer
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            Scanner console = new Scanner(System.in);
            System.out.print("Input file: ");
            String inputFileName = console.next();
            System.out.print("Output file: ");
            String outputFileName = console.next();
            FileReader reader = new FileReader(inputFileName);
            Scanner in = new Scanner(reader);
            PrintWriter out = new PrintWriter(outputFileName);
            int lineNumber = 1;
            
            while (in.hasNextLine())
            {
                String line = in.nextLine();
                out.println("/* " + lineNumber + "*/" + line);
                lineNumber++;
            }
            out.close();
        }
    }
    
     
  7. Unread #4 - Feb 8, 2011 at 10:42 AM
  8. Xeox
    Joined:
    Jun 17, 2010
    Posts:
    357
    Referrals:
    0
    Sythe Gold:
    0

    Xeox Forum Addict
    Banned

    Problem reading data and storing it

    Oh and I have to say this...this thread does not belong here, it belongs in the help forums (read fist stickey)

    but I really dont mind, it gives me something to do!
     
  9. Unread #5 - Feb 8, 2011 at 11:44 AM
  10. Insidious Insinuations
    Joined:
    Feb 5, 2011
    Posts:
    971
    Referrals:
    0
    Sythe Gold:
    0

    Insidious Insinuations Apprentice
    Banned

    Problem reading data and storing it

    I think it does, it's only if it's a non-programming related Java question.

    Like "HALP JAVA NO RUN FO ME PLZ"
     
  11. Unread #6 - Feb 8, 2011 at 1:47 PM
  12. Xeox
    Joined:
    Jun 17, 2010
    Posts:
    357
    Referrals:
    0
    Sythe Gold:
    0

    Xeox Forum Addict
    Banned

    Problem reading data and storing it

    oh haha ok, sorry for the post then >.<
     
  13. Unread #7 - Feb 8, 2011 at 2:30 PM
  14. blue_dolphin
    Joined:
    Feb 8, 2011
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0

    blue_dolphin Newcomer

    Problem reading data and storing it

    Nope. That's not true. What is in the original is correct, since it's shorthand for:
    Code:
    line = read.readline();
    line!=null
    To OP, as you print out the count of lines, it says it's 60, but you are only allocating an array of 12 elements. Also, every time you call readLine, it reads a line from the file continuously, so you never reload the file to start at the beginning. If you do not need to take file errors into account, you can just do:

    Code:
    while ((line = read.readLine()) != null) {
        String code = line;
        String description = read.readLine();
        double price = read.readLine();
        int stock = read.readLine();
        items[i] = new Item(code,description,price,stock);
        read.readLine();
    }
     
  15. Unread #8 - Feb 14, 2011 at 9:55 PM
  16. RyanSand
    Joined:
    Apr 8, 2010
    Posts:
    178
    Referrals:
    0
    Sythe Gold:
    0

    RyanSand Active Member

    Problem reading data and storing it

    put code=null just under Item[] items = new Item[12];
     
< Andriod MMO Project | webbrowser question >

Users viewing this thread
1 guest


 
 
Adblock breaks this site