• Post Reply Bookmark Topic Watch Topic
  • New Topic

not getting output  RSS feed

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

I am doing an assignment for school and I am not understanding why I am not seeing the output from a csv file containing data like author,publisher etc. or showing the list of books.
Any help would be appreciated.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got the keyword static so often?
How do you know you are reading from the file at all? Start by printing each line as you read it from the file. What on earth does the parseBooks method do?
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. I would try again and reply.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the parseBooks method is suppose to return a book object with all of its fields (instance variables) given a value.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:the parseBooks method is suppose to return a book object with all of its fields (instance variables) given a value.


Can you show us the correct version of the parseBooks() method? ... because with this version (that you shown), the answer to why "not getting output" would be because it doesn't compile.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that you posted won't even compile, because of various reasons:

In line 28 you are calling the parseBook method with a String, but as you can see in line 51 it expects a String[] (an array of strings, not a single string).

In line 51 you specified that the parseBook method returns a Book object, but it doesn't. It's not returning anything.

Since you can't even compile the code, you can't run it either. Please post your actual, compiling code - it's hard to help you if we don't know what your real program looks like.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:Any help would be appreciated...

Well one thing I notice is that your getScanner() method is "swallowing" an exception. This is generally very bad practise unless your program has some way of recovering from the error.

However, all you do is return null, which means that in the event of an IOException, your program will STILL fail, except that it will throw a NullPointerException instead of the one that it probably should.

If you're trying to avoid adding throws clauses all over the place, you can simply re-throw the exception as a RuntimeException or an Error, eg:however, even that assumes that all exceptions will be due to a bad filename, which may not be the case; so my preference would be to leave the System.err message out altogether.

HIH

Winston
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
real program:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on your new code listing, the answer is pretty simple... there isn't any real output because there is nothing to print.

You do open the file, but you do no parsing of the file. Your parseBook() method simply returns a null book, regardless of the file input lines. This means that your results list of books, is just a list of nulls.

Henry
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the task is to edit the parseBooks method to return a book object with all of its fields (instance variables) given a value.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:the task is to edit the parseBooks method to return a book object with all of its fields (instance variables) given a value.

OK, but that method has to return a Book, so where is that class? Or do you have to write that as well?

If so, I suggest you write one with a constructor that takes a "line" and returns a Book. Show us what you've tried.

It would also be useful to have a few examples of lines from your input file.

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

sorry for late response. the question is write the body of the parseBook() method in the BookCSVParser class, such that the parseBook() method returns a Book object with all of its fields (instance variables) given a value.

This is the code I have so far.



 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the way parseBook is called. It takes a String as a parameter and returns a Book. Your parseBook takes no parameters and returns nothing.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:the question is write the body of the parseBook() method in the BookCSVParser class, such that the parseBook() method returns a Book object with all of its fields (instance variables) given a value.

OK, but we still haven't seen your Book class yet. Has that also been written, or do you have to write it yourself?

Also (again): it would be nice to see some examples of your input lines.

We need information in order to help you Carolin, and simply repeating the same code doesn't help us much.

And just to remind you: we will help you write the method; we won't do it for you.

Winston
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the Book class. Make all its fields private. It should not be possible to write
myBook.title = ...;
anywhere outside the Book class. Also give the Book class a constructor which can initialise all its fields (or at least most of them).

Agree with Winston about showing us the Book class. When you designed your app you probably started with the Library and then wrote Book. When you write the code however, you should do it the other way round. You should write the Book class and get it working and tested before you can write the Library class.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I know you guys would not do it for me, some things are bit confusing for me. please see the book class which was provided and the AbstractBook class.




 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:
I know you guys would not do it for me, some things are bit confusing for me.


Perhaps it would be a good idea to make sure that parseBook() method (of the book parser class) actually compiles first... we can't give you a hint in the right direction, if you are not starting from a place where you can, at least, run your code.

Unless, of course, the issues are the compiler errors. In this case, I would recommend working on the compiler errors in order. Fix the first ones first, and then recompile. And if you are having issues with fixing the compiler error, perhaps you can tell us the error you are having trouble with, what have you tried to fix it, and we can give you a hint.

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

When I compile the original code parseBook() method of the book parser class this is the output.
null
null
null
null
null
null
null
null
null
null
null

The Book class was provided and uploaded earlier. the task at hand is to write the body of the parseBook() method in the BookCSVParser class, such that the parseBook() method returns a Book object with all its fields given a value.

I tried creating the Book object from the parseBook() method since I am supposed to just upload this BookCSVParser class as a deliverable to fulfil the task at hand. Is this approach wrong?

Really confused here.





 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Book class was provided and uploaded earlier. the task at hand is to write the body of the parseBook() method in the BookCSVParser class, such that the parseBook() method returns a Book object with all its fields given a value.



Does parseBook() return a Book? Does it take a value?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!