Your braces and semicolons are out of place. Look through your if () else if () block and check them.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Look at the structure of your class and main() method and compare that with your other assignments which do run. Also, look at the brace structure of any if-else block. The problems should be reasily apparent to you.
This is about as basic as debugging can get and you need to spot the problems on your own to learn. I've already told you what the nature of the problem is, so all you have to do is look at those blocks.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
I do not know what your teacher gave it to you and if you already covered Exceptions, but that what you get when you use BufferedReader getLine() .
Check this tutorial and you should be able to figure it out what to do with it. Main difference between yours code and code in the tutorials is that you read from command line and tutorial do it from a file
peter_budo
Code tags enforcer
15,432 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
or just add
throws IOException
underneath your main method declaration.
...
public static void main(String args[])
throws IOException {
String name;
...
that should work.
No, main really should not throw exceptions. How do you intend to handle an exception that is thrown beyond the scope of your execution?
The catch block should be placed within main() and handle the exception gracefully.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
It worked! Thx a lot! =) Now I can input my name and age, then it say's if im too young or i'm ready to vote. =)
thanks for the help everyone!
No, no, no, NO! Read the other posts. You do NOT want to throw the exception from main(). Don't even consider that an option. Yes, I know that it worked but it is NOT a habit you want to get into.
Add a try{} catch () {} block around the code that is using the reader as other posters have indicated.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
^
lol, i guess it's too late...
hehe, yeah I guess so. I had to try though.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847