Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing console input data to an object of a class..  RSS feed

 
Paul Johnston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Below is a snipet of code from a class I have made for data entry where the console retrieves the data from the keyboard using the readKB() method and stores it in a text file. I have another class called Fines that has the same variables and also does some calculation on these variables.
What I want to be able to do is open an object of the class Fines in the write() method of the LibraryFinesFile class. Then pass the values that have been inputted in the console through readKB(), to the Fines class. Then I would like to call the calculateTotalFine() method, to the object of the class Fines that has been created, perform the calculation in the code below and then write all the data to the file.
Is this possible? and is there a simple way of doing so? I am new to Java but I presume I should be using set and get methods to achieve this but I am not sure how to write the code for this or where to place the object of the class Fines. I am just trying to master the basics of Console I/O so the simplist way to accomplish this the better. Any help would be greatly appreciated. Thanks in advance, it's an awesome site.



Here also is my Fines class to clarify what I was meaning.


Also I have a second method in LibraryFinesFile which reads the data should I be using a get() method to retrieve my calculation in order to print in in the console, and if so how?
Any help would be greatly appreciated. Thanks in advance, it's an awesome site.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Fine object looks, um, fine! You'll probably want to make a Fine object to represent each overdue book. So maybe at the bottom of your loop you could say

Or you might want to make this a little easier on yourself by adding a constructor so you could do:

Now that you have one Fine object, what will you do with it? You can immediately call the getTotalFine() method to display the total fine. You might move the calculation from computeFine into getFine and just repeat the calculation every time somebody asks for the total. That way you're not storing the raw numbers AND the calculated result with a risk of getting them out of sync.
If you want to use your Fine objects later, you'll have to store them somewhere. Look at this Collections Crib Sheet and see if you find something that could hold a bunch of Fine objects. Maybe you could declare one of the sets or lists as a main class member variable, and add each new Fine to the collection. Then they'd be around for use in other methods later.
Does that get you moving again?
 
Paul Johnston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that, thats got me closer to sorting out my problem. I added a constructor to the Fines Class above, this is what I added,

I also added an object of this Fines Class to the LibraryFinesFile class by placing it in the while loop of the write() method and also placed the calls to the methods I needed. As follows,

The problem I now have is with the line

above as it doesn't seem to be valid and my code will not compile. I believe I have made a mistake declaring my variables but I'm not sure what I've actually done wrong.
Any help would again be greatly appreciated.
Thanks.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of error are you getting?
[ November 11, 2003: Message edited by: jason adam ]
 
Paul Johnston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error line I am getting is
�LibraryFinesFile.java�: �)� expected at line 27, column 36
There is also a red line under String book in the code indicating where the error is.
I am using Jbuilder to write the code.
Thanks.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to tell without seeing the whole code, but this line:
public void write(String Fines)
might be causing you problems. Your String parameter has the same name as your class you are creating an object for. Probably getting confused. Make that parameter lowercase "fines", and see if that works.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error line I am getting is
�LibraryFinesFile.java�: �)� expected at line 27, column 36
There is also a red line under String book in the code indicating where the error is.

But there's no "String book" in LibraryFinesFile, in the code you've shown above. You probably need to look at LibraryFinesFile.java, line 27, column 36, and see what's actually there. If it's not clear what going on, then show us a current copy of the code; we can't guess what it looks like from here.
More importantly though:

Evil. Bad. No. Never, ever do this. (Well, except for a few obscure cases like InterruptedException. Basically, never do this unless you know exactly why it's acceptable to do it in a particular case.) It hides errors without reporting them, which wastes time and defeats one of the primary benefits of using Java. Other programmers may be tempted to do violence to you if they discover you've written code like this. If you have a book which showed this as an example of acceptable error handling, burn it. If this code was auto-generated for you by JBuilder, uninstall JBuilder and don't use it. Or figure out how to configure JBuilder to put in
as a default instead; there's probably a template or something that controls this. You want an error message if somethign goes wrong, and the stack trace is often the most useful form this message can have. This advice probably has nothing at all to do with the problems you're currently experiencing, but it's very important in general, so don't overlook it while focusing on your current compiler error.
[ November 11, 2003: Message edited by: Jim Yingst ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*bops himself on the head* I shouldn't try to answer questions while fasting for 12 hours preparing for a physical.
Jim's right, the way you're creating the object is all wacky. You declare constructors like
public Fines( String b, int n, double r)
but whenever you create an object, you want to do something like
Fines f = new Fines( "test" , 1 , 1.50 );
or
String book = "Some Title";
int numOfDays = 1;
double price = 1.50;
Fines f = new Fines( book , numOfDays , price );
I'm going to go eat my sandwich and shut up now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!