• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

simple java.io question/error

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic