Results 1 to 14 of 14
- 07-24-2012, 01:59 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
Hi all. This is probably a simple fix but the answer is elluding me at the moment.
I have a JTable built on an AbstractTableModel (I needed some features of this API, I know it's harder but that's not the issue). The table model takes a String[] for the column names and an ArrayList<String> for the data set.
Now, the data source is a pipe-delimited .csv file. When I dump the data into the file it is formatted like this:
"fo"o" |"123" |"bar" |"abc" |"321"
"foo" |"def" |"bar" |"a"bc" |"321"
"bar" |"12"3" |"bar" |"123" |"321"
"def" |"123" |"bar" |"abc" |"321"
I read it like this:
The problem is, I need to strip the surrounding "", but keep the ones that are part of the actual data set.Java Code://get size of data source LineNumberReader lnr = new LineNumberReader(new FileReader(f)); lnr.skip(Long.MAX_VALUE); int ln = lnr.getLineNumber() - 1; // subtract 1 to account for the skipped row lnr = null; // Import CSV File into table model ArrayList<String> al = new ArrayList<String>(ln); while (c < ln) { al.add(s.nextLine());//.replaceAll("\"", """)); //clearly this doesn't meet the objective //al.add(s.nextLine().replace("\"", "")); c++; }
Any inexpensive methods to target only the surrounding double quotes?
I'd really appreciate any constructive input.
- 07-24-2012, 02:04 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
Split the string on the pipe. Trim each. Then replace the surrounding " (check the first and last characters, and replace if they are "). You could alternatively use a regular expression to group out the data internal to the quotes.
- 07-24-2012, 09:43 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
Similar to doWhile, split base on " |", then drop the first character of the first element and the last character of the last element.
Of course, someone will now come in with a regex that will do it in one go...:)Please do not ask for code as refusal often offends.
- 07-24-2012, 11:30 AM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
Yes I would use/advise to use the Scanner class with findWithinHorizon. read, filter, add string to the list and closing the stream in ~3-5 lines of code !
But it`s not clear to me, why do you need the number of lines? Only for the next while loop? I think that you don`t need the linenumberreader (first 4 lines)
- 07-24-2012, 07:10 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
I really appreciate the input, guys. So the issue is setting up the scanning loop/component to where I can get access to each data point and strip the doublequotes. I'm struggling to even get that far. I've looked high and low, but can't quite get around the following memory error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.ensureCapacity(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at pcInvpkg.SearchTest.getTable(SearchTest.java:289)
at pcInvpkg.SearchTest.<init>(SearchTest.java:108)
at pcInvpkg.gui2.build(gui2.java:40)
at pcInvpkg.gui2.main(gui2.java:27)
Using this code:
Tips/examples for a more efficient implementation?Java Code:Scanner scan = new Scanner(new File("C:\\PCQ\\A_R06.csv")); ArrayList<String> al = new ArrayList<String>(); scan.useDelimiter("\\|"); while(scan.hasNext()){ String[] list = scan.next().split("\\|"); for (String s : list) { //do string manipulation to String s here to remove "" al.add(s); } } scan.close(); scan = null;
- 07-24-2012, 07:24 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
People try to do too much with regular expressions, blindly ignoring the efficiency of it all; on top of that, they won't believe you when you explain to them that something can't be done with regular expressions, no matter when you prove the fact by using the pumping lemma; the only thing they do is laugh in a silly way. I dislike those regular expression monsters.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-24-2012, 08:12 PM #7
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
I love 'em ;)
No, I don't know how to avoid getting one empty element at position [0]Java Code:public class SplitPipeRemoveQuotes { public static void main(String[] args) { String[] inputs = { "\"fo\"o\" |\"123\" |\"bar\" |\"abc\" |\"321\"", "\"foo\" |\"def\" |\"bar\" |\"a\"bc\" |\"321\"", "\"bar\" |\"12\"3\" |\"bar\" |\"123\" |\"321\"", "\"def\" |\"123\" |\"bar\" |\"abc\" |\"321\"" }; String regex = "(^\")|(\"?\\s?\\|\"?)|(\"$)"; for (String input : inputs) { System.out.println(input); String[] outputs = input.split(regex); for (String output : outputs) { System.out.println("[" + output + "]"); } System.out.println("--------------------------"); } } }
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-24-2012, 08:26 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 07-24-2012, 09:25 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
AHHH soooo close!! I'm reading that once an array is instantiated it can't be altered as far as it's structure.. darn!
EDIT Got it!!
Snippet as implemented:
Using this little diddy:Java Code:while (c < ln) { String regex = "(^\")|(\"?\\s?\\|\"?)|(\"$)"; String[] line = s.nextLine().split(regex); String line2 = arrayToString2(line, "|"); //System.out.println(line2); al.add(line2); c++; } s.close(); s = null;
You guys are amazing, I really don't know what I'd do without this forum.Java Code:public static String arrayToString2(String[] a, String separator) { StringBuffer result = new StringBuffer(); if (a.length > 0) { result.append(a[0]); for (int i=2; i<a.length; i++) { result.append(separator); result.append(a[i]); } } return result.toString(); }Last edited by Redefine12; 07-24-2012 at 09:30 PM. Reason: solved
- 07-24-2012, 09:26 PM #10
- 07-24-2012, 09:29 PM #11
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
You can replaceAll("^\"", "") before splitting. I considered that cheating, because it uses two regexes.
Do you understand the structure of the regexes? If you don't, ask.Java Code:: : String regex = "(\"?\\s?\\|\"?)|(\"$)"; for (String input : inputs) { System.out.println(input); String[] outputs = input.replaceAll(("^\"", "").split(regex); : :
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-24-2012, 09:57 PM #12
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
I edited my above post with my hacky solution I gleaned from an example--it works, and using a seperate method is good OOP from my feeble grasp of it. I'm pretty impressed with the power of regex after seeing your implementation. I'm gonna devote a few hours to learning the concepts this afternoon. If you've got one, I'd love a link to a good explanation on regex (for dummies).
- 07-24-2012, 11:56 PM #13
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
Oh, interesting solutions. My idea was
Java Code:try (Scanner sc = new Scanner(new File("C:\\PCQ\\A_R06.csv"))) { while (sc.findWithinHorizon("\"(.+?)\"[\\|\\s]", 0) != null) { al.add(sc.match().group(1)); } }.gif)
I love them too
- 07-25-2012, 05:27 AM #14
Re: ArrayList<String> data cleanup for JTable- Strip surrounding doublequotes
The regex tutorials I use as references are:
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
JTable validate already existing JTable data and set Background colour issue
By pi4r0n in forum AWT / SwingReplies: 4Last Post: 04-02-2012, 07:57 PM -
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Code Cleanup
By zzpprk in forum Advanced JavaReplies: 2Last Post: 05-09-2010, 02:20 PM -
How to strip out comment tags
By mr.vinnie in forum New To JavaReplies: 3Last Post: 09-30-2009, 02:12 AM -
How do I strip down integers for future manipulation?
By frasifrasi in forum New To JavaReplies: 12Last Post: 06-14-2008, 02:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks