0

I can't find a proper solution to this problem:

I have a .csv file which has a list of values comma-separeted apart for the last one. When I try to assign those values to variables the last value of the first row comprehend the first value of the second row. I have tried with ",\n*" but it doesn't work.

Thanks in advance for you help.

Code:

Scanner inputStream = new Scanner(file).useDelimiter(",\\n*");

Csv File:

Thomson,Alfred,NY,00192838,USA

Vincent,Ramblè,PA,0033928283,FRANCE

What I get:

variable5 = USA Vincent

What I would like to get:

variable5 = USA

variable6 = Vincent

Community
  • 1
  • 1

3 Answers3

1

Just use a BufferedReader to read your file line by line, then inside the main loop use the StringTokenizer to tokenize a single line took by BufferedReader readLine method.

Alessandro Alessandra
  • 1,035
  • 12
  • 18
0

please remove the *. the following code appears to work

    String testpat = "abc,de\n\nasdfs,fffs,\nslsll\nss";
    Scanner scanner = new Scanner(testpat).useDelimiter(",\\n");

notice the pattern \n not \n*

StephenBoesch
  • 46,509
  • 64
  • 237
  • 432
  • Ihave tried it before, unfortunately it doesn't work, I get this error: _'Exception in thread "main" java.lang.OutOfMemoryError: Java heap space'_. I have tried thereafter to change the increase the Heap size available both from the eclipse.ini file and from the IDE menu, but I still get the same error. – JackTheDog Mar 10 '13 at 13:33
0

In your pattern, you either want a comma or an end of line combo. The best thing here would be a non-capturing group with an alternation. A character class might work if you never had to account for CRLF combinations, but that's not always a good assumption.

Here's an example. I'm using Clojure to drive the Java API, you should be able to get the gist if you take a minute to look at it, even if you're not familiar w/Clojure. The bits after the semi-colons are comments.

; define a function that will return a scanner on user input 
; with a given pattern
user=> (defn scanner [input delimiter] 
           (-> (java.util.Scanner. input) (.useDelimiter delimiter)))
#'user/scanner

; define the input
user=> (def input "Thomson,Alfred,NY,00192838,USA\nVincent,Ramblè,PA,0033928283,FRANCE")
#'user/input

; create the scanner 
;     (:?) is a non capturing group
;     the | in the middle tells the group to look for a or b
;     first alternative is a comma 
;     second alternative is a newline followed by 0 or 1 
;     carriage returns.
;     The javadoc for the java.util.Pattern class really helps
user=> (def myscanner (scanner input "(:?,|\n\r?)"))
#'user/myscanner

; quick/dirty way to call next on the scanner 10 times and print 
; the result of each call
user=> (dotimes [n 10] (println (.next myscanner)))
Thomson
Alfred
NY
00192838
USA
Vincent
Ramblè
PA
0033928283
FRANCE

If you really want to do CSV however, this problem has been solved many many times. There are lots of libraries out there that will handle some of the whacky parts of CSV. e.g. http://commons.apache.org/proper/commons-csv (this is just and example of one - you should evaluate it before using).

Good luck!

Bill
  • 12,022
  • 4
  • 34
  • 54
  • Thanks Bill!I managed to solve the problem just by adding |. So the working verion is `Scanner inputStream = new Scanner(file).useDelimiter(",|\n");` Thanks everybody for your help! – JackTheDog Mar 10 '13 at 14:08
  • @JackTheDog Glad you found it. The way this site works is that you accept the answer that is the most correct for you, and also upvote other ones that are good. Welcome to Stack Overflow, it's a great site. Very satisfying to help other people. – Bill Mar 10 '13 at 16:57