• Post Reply Bookmark Topic Watch Topic
  • New Topic

String split method.  RSS feed

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to split the contents of the text file and assign the value on the left of the | separator to a variable and the value on the right of the | separator to another variable. Thus I tried out a sample code to print all the values in the split [] first, and ended up with problems.

This is the content of the text file:
Crazed Boy|20
Hello|5
MSB|6.5




I keep getting IOException in my sample code, why is this so ? I assumed the split() method is supposed to output for the 1st iteration:
ss[0] = Crazed Boy
ss[1] = 20

Guess I am wrong in my understanding.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you change

to


This will let us see the exact error.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:I keep getting IOException in my sample code, why is this so ? I assumed the split() method is supposed to output for the 1st iteration...

The fact is that neither you nor we can know, because you "swallow" the Exception and simply print out "error".

My advice: Instead of a try...catch block, simply add "throws IOException" to your main() method.
That way, you'll get to SEE the stacktrace, and probably be able work it out for yourself.

HIH

Winston
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split() takes a regex and "|" is a metacharacter. To escape it you need a backslash "\". But backslash in a String is a metacharacter, so you need to escape the backslash. "\\"

So try:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!