Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: reading i n a text file

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reading i n a text file

    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.



     
    List<Courses> entries = new ArrayList<Courses>();
     
     
     
     
    		  BufferedReader br = null;
     
    		  try {
     
    		 String string;
    		 String[] array;
     
    		  br = new BufferedReader( new FileReader(
    		  getServletContext().getRealPath("/WEB-INF/undergrad_courses.txt")));
     
    		  while ((string = br.readLine()) != null) {
    			  if(string.startsWith("#")) {
    				  continue; 
    				  }
     
     
    		string.split(",").toString(); string.split(" "); entries.add(string");
     
    		  System.out.println(string); }
     
    		  } catch (IOException e) { e.printStackTrace(); } finally { try { if
    		  (br != null) br.close(); } catch (IOException ex) {
    		  ex.printStackTrace(); } }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: reading i n a text file

    One confusing thing I see in the code is that there are more than one statement on a line. That makes it harder to read and understand the code. There should only be one statement on a line.

    You need to read the API doc for the split() method. It returns an array with the separated parts of the String that was split. After calling split() the code needs to look in the array for the separate Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: reading i n a text file

    Cross posted here.

    Cross posting is allowed, but please provide a link so that time isn't wasted giving the same or even conflicting information if a different direction is taken.

Similar Threads

  1. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 07:47 AM
  2. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 04:32 PM
  3. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 01:24 AM
  4. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  5. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM