• Post Reply Bookmark Topic Watch Topic
  • New Topic

splitting string from csv  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to split the strings into components, put each token into appropriate variables, convert where necessary to integer and construct an object.
Please see my code below where I am editing the parseBook() method. Any help would be appreciated. I keep getting null as the output. For the output I would like to get the data separated as:
title: xxxx
author:xxx
publisher:xxxx

etc

 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that you have posted will not compile. so the result you are seeing are not from this code - maybe from some previous version which did compile.
 
Liutauras Vilda
Marshal
Posts: 4660
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Look at line 46. Probably you want to have there names1[0] as well as with others.

2. And what data type is "Int isbn = token[4];"?
Even if data type you were get right, that wouldn't work, as you get strings in your array after you split, so you'd need to parse them to integers. Read about method parseInt to see if it is suitable for you.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use ";" as the regex for split(). This will leave extraneous leading and/or trailing blank characters in your split strings You should useinstead.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
new Scanner( new File( filename ) )

Edit: never mind
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see my updated code.



when I compile getting:
constructor Book in class Book cannot be applied to given types;
Book b = new Book(title,author,publisher,genre,isbn,yop,numberofpages,totalsales);
^
required: String,String,ISBN,String,String,int,BookLength
found: String,String,String,String,String,String,String,String
reason: actual and formal argument lists differ in length
1 error

Process completed.

and when I run getting:
null
null
null
null
null
null
null
null
null
null
null

Process completed.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arguments being passed in to the Book constructor are of the wrong type and are in the wrong order.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:You use ";" as the regex for split(). This will leave extraneous leading and/or trailing blank characters in your split strings You should useinstead.

...or slightly better:
 
Anurag Verma
Ranch Hand
Posts: 170
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try matching the parameters of constructor of Book class, also for splitting why not give a try to StringTokenizer?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:I am trying to split the strings into components, put each token into appropriate variables, convert where necessary to integer and construct an object.
Please see my code below where I am editing the parseBook() method. Any help would be appreciated.

A couple of things I see:
1. parseBook() should be a method of your Book class - after all, that is the object that "knows" what it looks like.

2. You might also want a "checker" method that makes sure a line can be parsed as a Book, for example:and then your parseBook() method can be something like:
HIH

Winston

[Edit] corrected mistake at line 6 of first snippet.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anurag Verma wrote:Try matching the parameters of constructor of Book class, also for splitting why not give a try to StringTokenizer?


Quoting from the API for StringTokenizer:
"
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
"
It's been essentially deprecated for quite a while now.
 
Anurag Verma
Ranch Hand
Posts: 170
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave, I didn't knew that.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!