1

I have a very long string, which came from a file that my app downloads from the internet. Now after I've turned the file into a string, I split the string using String.split and then go through the file turning the things I've split into Strings (pieces), with the String "Description" usually containing multiple lines. After that I put those strings in a custom arraylist, then I use a custom adapter to display each item that I put in the arraylist, except it only displays the first line of Description. How can I make it so it displays all the lines in "Description?

How do I make it so that the read.line thinks of the "Description" pieces as one line, or is there some way to make it so that the line in "Description" are always combined?

Here's the content of the string that I'm trying to parse:

"","ZQ11ISF1","1","INSTRUCTIONAL SKILLS","MARKOVIC","","","","" "","TQS11QP2","1","COMPUTER PROGRAMMING 2","THORP","","","","" "","ZLF","1","FRIDAY LUNCH","MARKOVIC","","","","" "","PHS84","17","HEALTH & WELLNESS TERM 4 OF 8","REED","","","","" "","PQS11QPI","1","INDV. PHYS ED","MARKOVIC","","","","" "18342","SCS22","1","CHEMISTRY 2 OF 2","Mott","Chemistry HW for tomorrow","2014-06-03","Homework"," Hello chemistry students, We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources. Note: The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states: Acids donate protons (H+) Bases accept protons (H+) The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: Acids produce protons (H+) Bases produce hydroxide ions (OH-) Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions. Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids. Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. Neutrally, Mr. Mott " "","SXGPW","1","PBAT Science","","","","","" "","MXGRW","1","PBAT Algebra 2/Trig ","","","","","" "","EES84","4","ENGLISH 10 2 OF 2","GURALNICK","","","","" "18265","FSS84","8","SPANISH II 2 OF 2","RATCLIFFE","Tarea para 6/3","2014-06-03","Homework"," 1. The last page of your PBA review packets both readings. ANSWER QUESTIONS IN COMPLETE SENTENCES. " "18413","HGS44","10","GLOBAL STUDIES 10 2 OF 2","Anderson","Intro Paragraph and EDAC","2014-06-03","Homework"," For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs. Have a great night! " "18330","MRS22","7","ALGEBRA 2 / TRIG 2 OF 2","KLEIN","HW #28","2014-06-03","Homework"," A. pg. 803 #24,25,28,30,31 B. Check your answers here. www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf C. Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14. " 

Edit: I've figured out the read.line actually starts the while loop again each time it reads a new line, but it remembers what variable it was on so it creates a whole new "Description" String each time it encounters a new line (and new lines are only in the "Description" part of the String) and that gets added to the custom arraylist. So I need to figure out how to make it so it doesn't think that description is split onto different lines.

Edit: I think using Scanner would do away with my problem, but if I use scanner in a while loop it doesn't continue where it left off every time it loops, and it doesn't close itself when its done with the file.

Things I've Tried:

  • Make the string "Description" all one line (then the actual parser returns nothing)
  • Add .trim() to the end (Does nothing)
  • Removed the "check if "Type" was null", which added a number of ListView parts, which contained other lines of the String "Description"

Here is the code for the actual parser

Log.d("homework due today", Due_Today); InputStream is = new ByteArrayInputStream(Due_Today.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { while ((Due_Today = reader.readLine()) != null) { String[] part = Due_Today.split("\",\"", -1); int noOfItems = part.length; int counter=0; Description = counter<noOfItems ? part[counter] : ""; counter++; Band = counter<noOfItems ? part[counter] : ""; counter++; Number = counter<noOfItems ? part[counter] : ""; counter++; Class = counter<noOfItems ? part[counter] : ""; counter++; Teacher = counter<noOfItems ? part[counter] : ""; counter++; Title = counter<noOfItems ? part[counter] : ""; counter++; Date = counter<noOfItems ? part[counter] : ""; counter++; Type = counter<noOfItems ? part[counter] : ""; counter++; Data = counter<noOfItems ? part[counter] : ""; counter++; Description = Description.replaceAll("^\"|\"$", ""); Band = Band.replaceAll("^\"|\"$", ""); Number = Number.replaceAll("^\"|\"$", ""); Class = Class.replaceAll("^\"|\"$", ""); Teacher = Teacher.replaceAll("^\"|\"$", ""); Title = Title.replaceAll("^\"|\"$", ""); Date = Date.replaceAll("^\"|\"$", ""); Type = Type.replaceAll("^\"|\"$", ""); Data = Data.replaceAll("^\"|\"$", ""); Log.d("Data", Data); Log.d("Band", Band); Log.d("Number", Number); Log.d("Class", Class); Log.d("Teacher", Teacher); Log.d("Title", Title); Log.d("Date", Date); Log.d("Type", Type); Log.d("Description", Description); due_today_list.add(new Due_Today_List(Data, Band, Number, Class, Teacher, Title, Date, Type, Description)); 

Code for custom adapter

private void populateListView() { ArrayAdapter<Due_Today_List> adapter = new Due_TodayAdapter(); ListView list = (ListView)getView().findViewById(R.id.listView1); list.setAdapter(adapter); } public class Due_TodayAdapter extends ArrayAdapter<Due_Today_List> { public Due_TodayAdapter() { super(getActivity(), R.layout.item_view, due_today_list); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ convertView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false); holder = new ViewHolder(); holder.imageView = (ImageView)convertView.findViewById(R.id.item_iconclass); holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.item_texthomeworkdue); holder.DescriptionText = (TextView) convertView.findViewById(R.id.item_textdescription); holder.TeacherText = (TextView) convertView.findViewById(R.id.item_textteacher); holder.TypeText = (TextView) convertView.findViewById(R.id.item_texttype); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Due_Today_List currenthomeworkdue = due_today_list.get(position); if(currenthomeworkdue.getType() != null && !currenthomeworkdue.getType().isEmpty()){ Teacher = currenthomeworkdue.getTeacher().substring(0,1).toUpperCase()+currenthomeworkdue.getTeacher().substring(1).toLowerCase(); Description = currenthomeworkdue.getDescription().substring(5); if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("S")){ holder.imageView.setImageResource(R.drawable.science); } if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("M")){ holder.imageView.setImageResource(R.drawable.mathematics); } if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("H")){ holder.imageView.setImageResource(R.drawable.global_studies); } if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 2)).equals("FS")){ holder.imageView.setImageResource(R.drawable.spanish); } if(currenthomeworkdue.Band.substring(0, Math.min(currenthomeworkdue.Band.length(), 1)).equals("E")){ holder.imageView.setImageResource(R.drawable.english); } holder.HomeworkDueText.setText(currenthomeworkdue.getTitle()); holder.DescriptionText.setText(Description); holder.TeacherText.setText(Teacher); holder.TypeText.setText(currenthomeworkdue.getType()); return convertView; } else { View empty = getActivity().getLayoutInflater().inflate(R.layout.empty_item, parent, false); return empty; } } 

Here is what the Log returns

07-21 21:33:14.704: D/homework due today(8127): "","ZQ11ISF1","1","INSTRUCTIONAL SKILLS","MARKOVIC","","","","" 07-21 21:33:14.704: D/homework due today(8127): "","TQS11QP2","1","COMPUTER PROGRAMMING 2","THORP","","","","" 07-21 21:33:14.704: D/homework due today(8127): "","ZLF","1","FRIDAY LUNCH","MARKOVIC","","","","" 07-21 21:33:14.704: D/homework due today(8127): "","PHS84","17","HEALTH & WELLNESS TERM 4 OF 8","REED","","","","" 07-21 21:33:14.704: D/homework due today(8127): "","PQS11QPI","1","INDV. PHYS ED","MARKOVIC","","","","" 07-21 21:33:14.704: D/homework due today(8127): "18342","SCS22","1","CHEMISTRY 2 OF 2","Mott","Chemistry HW for tomorrow","2014-06-03","Homework"," 07-21 21:33:14.704: D/homework due today(8127):   07-21 21:33:14.704: D/homework due today(8127):   07-21 21:33:14.704: D/homework due today(8127): Hello chemistry students, 07-21 21:33:14.704: D/homework due today(8127): We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources. 07-21 21:33:14.704: D/homework due today(8127): Note: 07-21 21:33:14.704: D/homework due today(8127): The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:?  07-21 21:33:14.704: D/homework due today(8127): Acids donate protons (H+) 07-21 21:33:14.704: D/homework due today(8127): Bases accept protons (H+) 07-21 21:33:14.704: D/homework due today(8127): The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: 07-21 21:33:14.704: D/homework due today(8127): Acids produce protons (H+) 07-21 21:33:14.704: D/homework due today(8127): Bases produce hydroxide ions (OH-) 07-21 21:33:14.704: D/homework due today(8127): Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. 07-21 21:33:14.704: D/homework due today(8127):   07-21 21:33:14.704: D/homework due today(8127): Neutrally, 07-21 21:33:14.704: D/homework due today(8127): Mr. Mott 07-21 21:33:14.704: D/homework due today(8127):   07-21 21:33:14.704: D/homework due today(8127): " 07-21 21:33:14.704: D/homework due today(8127): "","SXGPW","1","PBAT Science","","","","","" 07-21 21:33:14.704: D/homework due today(8127): "","MXGRW","1","PBAT Algebra 2/Trig ","","","","","" 07-21 21:33:14.704: D/homework due today(8127): "","EES84","4","ENGLISH 10 2 OF 2","GURALNICK","","","","" 07-21 21:33:14.704: D/homework due today(8127): "18265","FSS84","8","SPANISH II 2 OF 2","RATCLIFFE","Tarea para 6/3","2014-06-03","Homework"," 07-21 21:33:14.704: D/homework due today(8127):  1. The last page of your PBA review packets both readings. 07-21 21:33:14.704: D/homework due today(8127):   07-21 21:33:14.704: D/homework due today(8127): ANSWER QUESTIONS IN COMPLETE SENTENCES. 07-21 21:33:14.704: D/homework due today(8127): " 07-21 21:33:14.704: D/homework due today(8127): "18413","HGS44","10","GLOBAL STUDIES 10 2 OF 2","Anderson","Intro Paragraph and EDAC","2014-06-03","Homework"," 07-21 21:33:14.704: D/homework due today(8127):  For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs. 07-21 21:33:14.704: D/homework due today(8127): Have a great night! 07-21 21:33:14.704: D/homework due today(8127): " 07-21 21:33:14.704: D/homework due today(8127): "18330","MRS22","7","ALGEBRA 2 / TRIG 2 OF 2","KLEIN","HW #28","2014-06-03","Homework"," 07-21 21:33:14.704: D/homework due today(8127):  A.  pg. 803 #24,25,28,30,31 07-21 21:33:14.704: D/homework due today(8127): B.  Check your answers here. 07-21 21:33:14.704: D/homework due today(8127): www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf 07-21 21:33:14.704: D/homework due today(8127): C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14.  07-21 21:33:14.704: D/homework due today(8127): " 07-21 21:33:14.704: D/homework due today(8127): 07-21 21:33:14.714: D/Band(8127): ZQ11ISF1 07-21 21:33:14.714: D/Number(8127): 1 07-21 21:33:14.714: D/Class(8127): INSTRUCTIONAL SKILLS 07-21 21:33:14.714: D/Teacher(8127): MARKOVIC 07-21 21:33:14.714: D/Band(8127): TQS11QP2 07-21 21:33:14.714: D/Number(8127): 1 07-21 21:33:14.714: D/Class(8127): COMPUTER PROGRAMMING 2 07-21 21:33:14.714: D/Teacher(8127): THORP 07-21 21:33:14.714: D/Band(8127): ZLF 07-21 21:33:14.714: D/Number(8127): 1 07-21 21:33:14.714: D/Class(8127): FRIDAY LUNCH 07-21 21:33:14.714: D/Teacher(8127): MARKOVIC 07-21 21:33:14.714: D/Band(8127): PHS84 07-21 21:33:14.724: D/Number(8127): 17 07-21 21:33:14.724: D/Class(8127): HEALTH & WELLNESS TERM 4 OF 8 07-21 21:33:14.724: D/Teacher(8127): REED 07-21 21:33:14.724: D/Band(8127): PQS11QPI 07-21 21:33:14.724: D/Number(8127): 1 07-21 21:33:14.724: D/Class(8127): INDV. PHYS ED 07-21 21:33:14.724: D/Teacher(8127): MARKOVIC 07-21 21:33:14.724: D/Band(8127): SCS22 07-21 21:33:14.724: D/Number(8127): 1 07-21 21:33:14.724: D/Class(8127): CHEMISTRY 2 OF 2 07-21 21:33:14.734: D/Teacher(8127): Mott 07-21 21:33:14.734: D/Title(8127): Chemistry HW for tomorrow 07-21 21:33:14.734: D/Date(8127): 2014-06-03 07-21 21:33:14.734: D/Type(8127): Homework 07-21 21:33:14.734: D/Description(8127): 18342 07-21 21:33:14.734: D/Description(8127):   07-21 21:33:14.744: D/Description(8127):   07-21 21:33:14.754: D/Description(8127): Hello chemistry students, 07-21 21:33:14.754: D/Description(8127): We did the acid base conductivity lab today in class.  There are 29 questions and 11 summary questions.  I stated at the beginning of class today that there would be no homework except for determining who your partner(s) will be for the PBA presentation.  I’ll stand by that.  In class tomorrow we will finish the questions.  I hope you’ve started to research/write your PBA paper.  Remember to write down the information (title, author, web address, webpage title, date of publication if possible, publisher and location if book)  of any source you use so that you can put it into you bibliography at the end.  Your textbook would be a wise choice as one of your sources. 07-21 21:33:14.764: D/Description(8127): Note: 07-21 21:33:14.774: D/Description(8127): The Bronsted-Lowry Theory (1932) of Acids and Bases (which we have been adopting in class so far) states:?  07-21 21:33:14.774: D/Description(8127): Acids donate protons (H+) 07-21 21:33:14.784: D/Description(8127): Bases accept protons (H+) 07-21 21:33:14.784: D/Description(8127): The Arrhenius Theory (1887) of Acids and Bases (which this lab introduces on the back of the second page) states: 07-21 21:33:14.794: D/Description(8127): Acids produce protons (H+) 07-21 21:33:14.804: D/Description(8127): Bases produce hydroxide ions (OH-) 07-21 21:33:14.804: D/Description(8127): Historically, the Bronsted-Lowry Theory came second and it doesn’t contradict the Arrhenius Theory. It only broadens it because the Arrhenius Theory limits bases to chemicals with OH- ions.  Some chemicals, however, like NH3 (which don’t have OH- ions) do act like bases by neutralizing acids.   Neutralize means that an acid and a base combine to form a salt, sometimes water would need to be evaporated to extract the salt. 07-21 21:33:14.804: D/Description(8127):   07-21 21:33:14.814: D/Description(8127): Neutrally, 07-21 21:33:14.814: D/Description(8127): Mr. Mott 07-21 21:33:14.824: D/Description(8127):   07-21 21:33:14.824: D/Band(8127): SXGPW 07-21 21:33:14.824: D/Number(8127): 1 07-21 21:33:14.824: D/Class(8127): PBAT Science 07-21 21:33:14.824: D/Band(8127): MXGRW 07-21 21:33:14.824: D/Number(8127): 1 07-21 21:33:14.824: D/Class(8127): PBAT Algebra 2/Trig 07-21 21:33:14.824: D/Band(8127): EES84 07-21 21:33:14.824: D/Number(8127): 4 07-21 21:33:14.824: D/Class(8127): ENGLISH 10 2 OF 2 07-21 21:33:14.824: D/Teacher(8127): GURALNICK 07-21 21:33:14.824: D/Band(8127): FSS84 07-21 21:33:14.824: D/Number(8127): 8 07-21 21:33:14.824: D/Class(8127): SPANISH II 2 OF 2 07-21 21:33:14.824: D/Teacher(8127): RATCLIFFE 07-21 21:33:14.824: D/Title(8127): Tarea para 6/3 07-21 21:33:14.824: D/Date(8127): 2014-06-03 07-21 21:33:14.824: D/Type(8127): Homework 07-21 21:33:14.824: D/Description(8127): 18265 07-21 21:33:14.834: D/Description(8127):  1. The last page of your PBA review packets both readings. 07-21 21:33:14.834: D/Description(8127):   07-21 21:33:14.834: D/Description(8127): ANSWER QUESTIONS IN COMPLETE SENTENCES. 07-21 21:33:14.844: D/Band(8127): HGS44 07-21 21:33:14.844: D/Number(8127): 10 07-21 21:33:14.844: D/Class(8127): GLOBAL STUDIES 10 2 OF 2 07-21 21:33:14.844: D/Teacher(8127): Anderson 07-21 21:33:14.844: D/Title(8127): Intro Paragraph and EDAC 07-21 21:33:14.844: D/Date(8127): 2014-06-03 07-21 21:33:14.844: D/Type(8127): Homework 07-21 21:33:14.844: D/Description(8127): 18413 07-21 21:33:14.844: D/Description(8127):  For homework tonight, please finish your introductory paragraph and any remaining EDAC sheets for your essay. Come to class tomorrow with a printed copy (in print, no phones!!!) of your intro and EDAC sheets finished for each of your body paragraphs. 07-21 21:33:14.844: D/Description(8127): Have a great night! 07-21 21:33:14.854: D/Band(8127): MRS22 07-21 21:33:14.854: D/Number(8127): 7 07-21 21:33:14.854: D/Class(8127): ALGEBRA 2 / TRIG 2 OF 2 07-21 21:33:14.854: D/Teacher(8127): KLEIN 07-21 21:33:14.854: D/Title(8127): HW #28 07-21 21:33:14.854: D/Date(8127): 2014-06-03 07-21 21:33:14.854: D/Type(8127): Homework 07-21 21:33:14.854: D/Description(8127): 18330 07-21 21:33:14.854: D/Description(8127):  A.  pg. 803 #24,25,28,30,31 07-21 21:33:14.854: D/Description(8127): B.  Check your answers here. 07-21 21:33:14.854: D/Description(8127): www.beaconschool.org/~sklein/Algebra2/a2hwtriglos.pdf 07-21 21:33:14.864: D/Description(8127): C.  Create the FIRST half of your Study Sheet for the Trig Test on Monday, 6/9/14.  07-21 21:33:14.864: D/Description(8127): 

Edit: So I've resorted to trying to add a while loop inside the parser while loop which looks to gather up all the lines of "Description" and add them all to one string. I have not had a lot of luck with it, since it never seems to do what I want it to do. It skips all the first part of the file, which is what it should do, and then it gets the "Description" part together, but after that everything goes crazy and it adds all this extra stuff to the StringBuilder. Anyway here is the new parser code and its output in the log.

StringBuilder DescriptionAll = new StringBuilder(); InputStream is = new ByteArrayInputStream(Due_Today.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { while ((Due_Today = reader.readLine()) != null) { String[] part = Due_Today.split("\",\"", -1); int noOfItems = part.length; int counter=0; DescriptionCheck = counter<noOfItems ? part[counter] : "";counter++; if (!Due_Today.contains("\",\"") && DescriptionCheck != null && !DescriptionCheck.isEmpty() ){ while ((Due_Today = reader.readLine()) != null & !DescriptionCheck.contains("\",\"")) { Log.d("DescriptionCheck", DescriptionCheck ); String[] parter = Due_Today.split(System.getProperty("line.separator"), -1); Description = parter[0]; DescriptionAll.append((System.getProperty("line.separator") + Description)); Log.d("descriptioncheck", Description ); if (Description.contains("\",\"")) { Log.d("BROKEN?", "YES" ); break; } } } Band = counter<noOfItems ? part[counter] : ""; counter++; Number = counter<noOfItems ? part[counter] : ""; counter++; Class = counter<noOfItems ? part[counter] : ""; counter++; Teacher = counter<noOfItems ? part[counter] : ""; counter++; Title = counter<noOfItems ? part[counter] : ""; counter++; Date = counter<noOfItems ? part[counter] : ""; counter++; Type = counter<noOfItems ? part[counter] : ""; counter++; Data = counter<noOfItems ? part[counter] : ""; counter++; Band = Band.replaceAll("^\"|\"$", ""); Number = Number.replaceAll("^\"|\"$", ""); Class = Class.replaceAll("^\"|\"$", ""); Teacher = Teacher.replaceAll("^\"|\"$", ""); Title = Title.replaceAll("^\"|\"$", ""); Date = Date.replaceAll("^\"|\"$", ""); Type = Type.replaceAll("^\"|\"$", ""); Data = Data.replaceAll("^\"|\"$", ""); Description = DescriptionAll.toString(); Description = Description.replaceAll("^\"|\"$", ""); Log.d("Data", Data); Log.d("Band", Band); Log.d("Number", Number); Log.d("Class", Class); Log.d("Teacher", Teacher); Log.d("Title", Title); Log.d("Date", Date); Log.d("Type", Type); Log.d("Description", Description); Log.d("DescriptionAll", Description); due_today_list.add(new Due_Today_List(Data, Band, Number, Class, Teacher, Title, Date, Type, Description)); 

And here is what the log looks like now, pure craziness: http://pastebin.com/M8ZT07ii

Edit:

Here's a screenshot of what the app looks like when everything is sorted perfectly except "Description" (Where only the first line of Description for each one shows up), this uses the first code I tried (without the new while loop"

enter image description here

And here you can see that it only shows the first line of "Description

enter image description here

Here's what the app looks like when I use the new code with the while loop

enter image description here

Here's what the "Description" output is for the new code with the while loop, as you can see as a result of the while loop I remove the first line of "Description", which is ok because it's just a placeholder

enter image description here enter image description here

enter image description here enter image description here

28
  • I added it, even though I don't think that's what the problem is, but whatever helps. I could definitely be wrong about that though. Commented Jul 22, 2014 at 2:11
  • Looks like all the description is fine, it is being output in the log. Maybe your "TextView" is only one line tall? So the rest gets cut-off? Maybe is simply a display issue? Commented Jul 22, 2014 at 2:34
  • I tested it before with a very long string of words, and it didn't get cut off. So it shouldn't be a display issue. Commented Jul 22, 2014 at 2:50
  • @I'm_With_Stupid after reading all ur decription m bit confused ..can u expain inmore details in chat i would like to answer this question today Commented Jul 25, 2014 at 8:18
  • @adcom thanks for wanting to help. Basically I am trying to figure out a way to separate each piece of the string above by the delimiter [","], and convert that to a string. My parser will loop through the file, and convert something to a string based on the pattern of the items in the string. But when my parser encounters an item which has multiple lines, like "Description" does, it devotes a string to each line, it doesn't turn all the lines into one string. So I need to figure out how to make it so all the lines in Description are one string. Commented Jul 25, 2014 at 19:04

2 Answers 2

1
+50

I think that the problem comes from the fact that you are using readLine to read your file. By using this function the buffer is read until a line feed "\n" or carriage return "\r" is found. However, What you want is to split the string each time the occurrence ',' is found.

Try to use read instead to read through Due_Today. This is only a pseudo-code:

int value = 0; int counter = 0; String str = ""; StringBuilder strb = new StringBuilder(); // Read until the end of the file while((value = reader.read()) != -1) { // Convert int to a character char c = (char)value; strb.append(c); // If the separator is found if (c == ','){ str = strb.toString().replaceAll("^\"|\"$", ""); switch (counter) { /* Do things according to the value of counter: Read description, data, title.. according to the data structure of what you are trying to parse Example: case 1: Description = str; break; case 2: Band = str; break; */ } // Prepare to read a new string and initialize the string builder strb.setLength(0); counter ++; } } 

I do know exactly what is the data structure of the file you are parsing but you may have some problems if you must read more than one object in one file. I noticed that the "description" can contains some comma which will be seen as a separator. Therefore you can either read only one object and say "Once I start reading the description, read the file until the end no matter if there are some commas" or change the separator by a character that is not used in the content of your strings.

Hope this helps

EDIT

1) What about something like this to search for your separator? It might not be the perfect method but at least it is fast as it does not search for the entire separator pattern at each character.

while((value = reader.read()) != -1) { // Convert int to a character char c = (char)value; // Searching for the separator if (c == ','){ if (state == 1) { state = 2; } else { state = 0; strb.append(c); } } else if (c == '"'){ if (state == 2){ // The separator if found, read the string processElement(strb, counter); // Prepare to read a new string state = 0; counter ++; } else { state = 1; strb.append(c); } } else { strb.append(c); } } // Process the last element processElement(strb, counter); public void processElement(StringBuilder strb, int counter){ String str = strb.toString().replaceAll("^\"|\"$", ""); System.out.println("String to process= " + str); /* Do something with it here.. switch (counter) { case 1: Description = str; break; case 2: Band = str; break; ... } */ // Initialize the string builder strb.setLength(0); } 

2) If you are willing to read to file until the end, you can do it that way:

while((value = reader.read()) != -1) { // Convert int to a character char c = (char)value; strb.append(c); } String str = strb.toString(); 
Sign up to request clarification or add additional context in comments.

10 Comments

Unfortunately this won't work because I'm not splitting on just comma, I'm splitting on quote comma quote (","), since this is three characters it won't work. Secondly .read() returns an integer, which I would rather not use. But your definitely on the right track, I've been trying to find something that does read it all instead of line by line but I can't. But thank you for the answer.
This does separate all of them, but for some reason it stops right before the last description, the one which begins with: A. pg. 803 #24,25,28,30,31
My mistake, I forgot to process the last element. The post has been edited ;)
one more thing, would their be a way to repeat this method 8 times, but each time "str" is set to a different variable?
You can pass another variable to the function to specify to which variable you want to set the String. I have put an switch case example in my post.
|
1

So I think you are complicating the parsing of the String into smaller segments way more then it needs to be.

Here is what I would do:

A. I would use the Files Class specifically the readAllBytes method to read the contents of my entire file into a byte array.

Like so

import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); byte[] myArray = Files.readAllBytes(path); 

B. After reading in the file I would convert the byte[] into a String.

Like so

String myString = new String(myArray); 

C. Finally I would use String function Split.

String[] myStringArray = myString.spilt(","); 

myStringArray will hold all your relevant "substrings", to do with as you please.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.