Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
Problem reading data and storing it
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-08-2011, 06:39 AM
Done, Thanks.
Cracker Head $25 USD Donor Sythe Verified User
 
Join Date: Mar 2007
Posts: 22,548
Send a message via Skype™ to Gohan
UWotM8? Gohan has AIDS Homosex
Default 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 ‘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



,,,,,,
__________________

Last edited by Gohan : 02-08-2011 at 06:42 AM.
Reply With Quote
  #2  
Old 02-08-2011, 02:21 PM
Fate's Avatar
Apprentice
 
Join Date: Apr 2005
Posts: 774
Default Re: 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[i] 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.

Last edited by Fate : 02-08-2011 at 02:21 PM.
Reply With Quote
  #3  
Old 02-08-2011, 03:34 PM
Xeox's Avatar
Forum Addict
$25 USD Donor
 
Join Date: Jun 2010
Location: Watertown, CT
Posts: 346
Send a message via MSN to Xeox
Cards Re: Problem reading data and storing it

Quote:
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();
    }
}
__________________
Reply With Quote
  #4  
Old 02-08-2011, 03:42 PM
Xeox's Avatar
Forum Addict
$25 USD Donor
 
Join Date: Jun 2010
Location: Watertown, CT
Posts: 346
Send a message via MSN to Xeox
Default Re: 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!
__________________
Reply With Quote
  #5  
Old 02-08-2011, 04:44 PM
Insidious Insinuations's Avatar
Apprentice
 
Join Date: Feb 2011
Location: Ontario
Posts: 970
Default Re: Problem reading data and storing it

Quote:
Originally Posted by Xeox View Post
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"
Reply With Quote
  #6  
Old 02-08-2011, 06:47 PM
Xeox's Avatar
Forum Addict
$25 USD Donor
 
Join Date: Jun 2010
Location: Watertown, CT
Posts: 346
Send a message via MSN to Xeox
Default Re: Problem reading data and storing it

oh haha ok, sorry for the post then >.<
__________________
Reply With Quote
  #7  
Old 02-08-2011, 07:30 PM
Newcomer
 
Join Date: Feb 2011
Posts: 5
Default Re: Problem reading data and storing it

Quote:
Originally Posted by Xeox View Post
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();
}
Reply With Quote
  #8  
Old 02-15-2011, 02:55 AM
RyanSand's Avatar
Active Member
 
Join Date: Apr 2010
Posts: 165
Send a message via MSN to RyanSand
Default Re: Problem reading data and storing it

put code=null just under Item[] items = new Item[12];
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 02:11 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1