File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes reading in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "reading in a text file" Watch "reading in a text file" New topic
Author

reading in a text file

John nava
Greenhorn

Joined: Jan 22, 2014
Posts: 10
i need some help with trying to read in a text file. the directions are:
A line that starts with # is a comment line. Comment lines and empty lines should be ignored.
Each course occupies one line. Each course has three fields: course code, course title, and prerequisites. Fields are separated by comma, and prerequisites are separated by white space(s). Note that some courses like CS120 do not have prerequisites.

i cant get it to work at all , for some reason the splits dont work and i dont know how to make it read in 3 fields. This is the final part of my project thats due in 2 hours and ive been at trying to get it to work since last night . im stumped, ive done simple readin files but none this complicated.



Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 159
    
    3

The String#split method returns an array of Strings, but you aren't doing anything with it.
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 159
    
    3

Here is an example. Pay attention to lines 5 and 11.
John nava
Greenhorn

Joined: Jan 22, 2014
Posts: 10
alright i see what you mean about the string array. now i got it to this point . but i get an error at this line
String[] prereq = eachCourse[3].split(" ");

Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 159
    
    3

You need to read-up on how split works: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29

In line 13 you are specifying a limit of 4. Is that correct for your application?

In line 14, you are expecting the prereq's to be in the 4th element of the array. Is that correct for your application?
John nava
Greenhorn

Joined: Jan 22, 2014
Posts: 10
doh forgot to post the updated code , sorry about that. but yeah my limit would be 3 , since theres 3 subjects( code,title, prerequisites). and i want prereqs to be the 3rd element in the array. after titles.


John nava
Greenhorn

Joined: Jan 22, 2014
Posts: 10
the error i get is SEVERE: Allocate exception for servlet cs320Homework1.servlet.CourseDisplay
java.lang.ArrayIndexOutOfBoundsException: 2
at cs320Homework1.servlet.CourseDisplay.init(CourseDisplay.java:47)

hmm alright quick question. my contructor takes in 4 things.


I dont use the id in my course display. i still have to account for it right , even though i dont use it. my text file only has 3 columns ( code,title,prereq). so i would still count it as a limit of 4?

my thinking with the limit was that i have 3 columns i want to display(code,title,pre)
String[] eachCourse=string.split(",",3);

and here im expecting my prerequisites to be in the 3rd element of the array.
String[] prereq = eachCourse[2].split(" ");

ehh im all confused with the out of bounds stuff , just trying to think it out.
Ron McLeod
Ranch Hand

Joined: Feb 12, 2013
Posts: 159
    
    3

If the id parameter doesn't come from the course information, then I guess you need to get in from somewhere else. Perhaps it should just be a unique incrementing value?

I suspect the problem you are seeing now, is that you are reading course information which has less than three fields (a course without prereq's?). You will need to deal with this when you parse the data. Either discard a line which doesn't have the correct number of fields, or use a default value for information not provided.

It is worth spending more time understanding how split will work for you, with more or less data than expected.


Input: one,two,three
Number of parts: 3
Parts:
#0: one
#1: two
#2: three

Input: one,,three
Number of parts: 3
Parts:
#0: one
#1:
#2: three

Input: one,two,three,four,five
Number of parts: 3
Parts:
#0: one
#1: two
#2: three,four,five

Input: one,two,three,four five
Number of parts: 3
Parts:
#0: one
#1: two
#2: three,four five

Input: one,two,
Number of parts: 3
Parts:
#0: one
#1: two
#2:

Input: one,two
Number of parts: 2
Parts:
#0: one
#1: two

Input: one?two?three
Number of parts: 1
Parts:
#0: one?two?three

John nava
Greenhorn

Joined: Jan 22, 2014
Posts: 10
i get that example , i just cant see it in my project. maybe becaue ive slept 2 hours in since last night trying to figure this out. but im gonna wave the white flag , my prjoect is due ina hour and im screwed. soooo close , but its like my brain just shut off. i really appreciate the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading in a text file
 
Similar Threads
Servlet check box
Help reading File
Reading a text file
Reading data from a delimited file in to a vector
Code critique: CSV to Tab-delimited