• Log in or sign up for an account here: https://www.sythe.org/login

Problem reading data and storing it

Gohan

Legend

Retired Sectional Moderator
Cracker Head
$25 USD Donor
Prince Yobabo
Joined
Mar 16, 2007
Sythe Gold
248
Messages
23,694
Location
Georgia
Discord Unique ID
100075291572998144
Sythe Awards 2010 Winner
Sythe Awards 2011 Winner
Spam Forum Participant
PHP Shit Show 2k26
Rust Player
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




,,,,,,
 
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.
 
while ((line = read.readLine()) != null)

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();
    }
}
 
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!
 
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!

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

Like "HALP JAVA NO RUN FO ME PLZ"
 
I think you ment to write:

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

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();
}
 
put code=null just under Item[] items = new Item[12];
 
duelarena.gg
Back
Top