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

simple java.io question/error  RSS feed

 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my code:



Here's my error:


What's my problem here? I've tried a number of different things to get rid of the error, but none worked.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read javadoc API for BufferedReader.readLine()?

Give it a look here

You'll realize that you are using the wrong signature for the readLine() method and you will solve your problem in the blink of an eye.
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see my problem. Even though I read the java API for it before and after you said it, I didn't see my problem because I was confusing the readLine() method with the parse Integer class. So now I have:
Now I also noticed that it needs throws IOException. This is where I'm caught now, and it probably deals with things that I've not touched yet in Head First Java (I'm on page 110), though I did touch on it in my high school course. Anyway, what I'm not sure about is what kind of method I need. For instance, isn't void supposed to return nothing? I've never actually programmed something with a class that does stuff with methods and a testdrive class. That is specifically what I'm trying to accomplish in this simple program; my goal is to have objects talking to objects.
[ May 22, 2006: Message edited by: Daniel Lucas ]
 
abhishek jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

If I am getting you right, I think ou have problem doing " Exception Handling". That is simple stuff (you may not feel the the same bcoz you have not gone thru), you can do it in two ways ..

o either throw the Exception by saying
" .... method() throws IOExcetion { ...}"
if you use this approach, then again you have to do the Exception Handling in the calling method (i.e. from the method you are calling this method).
EX:
void weight() throws IOException
{
System.out.println("Enter the weight of the dog: ");
strWeight = in.readLine();
d.weight = Integer.parseInt(strWeight);
}
or

o by adding a "try ... catch" block.
EX:
void weight()
{
try
{
System.out.println("Enter the weight of the dog: ");
strWeight = in.readLine();
d.weight = Integer.parseInt(strWeight);
}
catch (Exception e)
{
System.oout.println("Exception occured " + e.printStackTrace);
}
}

Hope, this will help you. If not, can you eloborate your problem statement ??
 
Daniel Lucas
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you're saying here. I'm going to continue reading in my book then post back when I find a solution. I managed to get the Dog class to compile, and I even got the DogTestDrive class I just made to compile, but it doesn't work properly. I gave an error when I tried to run it, saying there was a problem with main(). Anyway, I'll post back once I read all the stuff about the readLine() method, and maybe try and catch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!