• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

splitting string from csv

 
Ranch Hand
Posts: 83
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 4792
600
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try
new Scanner( new File( filename ) )

Edit: never mind
 
Carolin Sha
Ranch Hand
Posts: 83
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arguments being passed in to the Book constructor are of the wrong type and are in the wrong order.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try matching the parameters of constructor of Book class, also for splitting why not give a try to StringTokenizer?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave, I didn't knew that.
reply
    Bookmark Topic Watch Topic
  • New Topic