Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading in a text file

 
John ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 864
46
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String#split method returns an array of Strings, but you aren't doing anything with it.
 
Ron McLeod
Bartender
Pie
Posts: 864
46
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example. Pay attention to lines 5 and 11.
 
John ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 864
46
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 864
46
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ortega
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic