Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Sheriff
Posts: 3919
542
Android 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.
 
Marshal
Posts: 8432
606
Mac OS X VI Editor BSD Linux
  • 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: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 7113
184
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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic