Else If statement

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2007
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

Else If statement

 
0
  #1
Sep 6th, 2007
Hello everyone, I just need help on this code. I can't debug it, I'm just new in Java and this is just my fifth assignment so I'm really having a hard time.

Here's my code:

  1. import java.io.*;
  2. public class input
  3. {
  4. public static InputStreamReader Reader=new
  5. InputStreamReader (System.in);
  6. public static BufferedReader input=new
  7. BufferedReader(Reader);
  8. public static void main(String args[]);
  9. String name;
  10. int age;
  11. double salary;
  12. {
  13. System.out.println("Enter name:");
  14. name=input.readline();
  15. System.out.println("Enter age:");
  16. age=Integer.parseInt(input.readLine());
  17. if(age<=18)
  18. System.out.println("Too young!");
  19. }
  20. else if (age>=18);
  21. {
  22. System.out.println("You're ready to vote!");
  23. }
  24. }

the Error says that:
illegal start of type line 20
<identifier> expected line 20

  1. else if (age>=18);
Can anyone please tell me how to debug it? =/
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

 
0
  #2
Sep 6th, 2007
Your braces and semicolons are out of place. Look through your if () else if () block and check them.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

Re: Else If statement

 
0
  #3
Sep 6th, 2007
uhmmm.. I'm sorry, but I already tried to move the braces at the "else if" line. Still I can't let it run. =(
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

 
0
  #4
Sep 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

Re: Else If statement

 
0
  #5
Sep 6th, 2007
ok.. thx.. I'll check them.. =)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

Re: Else If statement

 
0
  #6
Sep 6th, 2007
arg.. a new error occured. >.< this is my only notes/guide for the if else statement. =(
  1. import java.io.*;
  2. public class input{
  3. public static InputStreamReader Reader=new
  4. InputStreamReader(System.in);
  5. public static BufferedReader input=new
  6. BufferedReader(Reader);
  7. public static void main(String args[]){
  8. String name;
  9. int age;
  10. {
  11. System.out.println("Enter name:");
  12. name=input.readLine();
  13. System.out.println("Enter age:");
  14. age=Integer.parseInt(input.readLine());
  15. if (age<=17){
  16. System.out.println("Too young!");
  17. } else if(age>=18){
  18. System.out.println("You're ready to vote!");
  19. }
  20. }
  21. }
  22. }
The error says:
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 12
  1. name=input.readLine();
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 14
  1. age=Integer.parseInt(input.readLine());
It seems like my teacher forgot something or I copied the code wrong. =(
Last edited by rapperhuj; Sep 6th, 2007 at 10:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,196
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Else If statement

 
0
  #7
Sep 7th, 2007
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

 
0
  #8
Sep 7th, 2007
Using Scanner to read the input would be easier and you wouldn't have to mess with the ioexception.
http://java.sun.com/javase/6/docs/ap...l/Scanner.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: thenava is an unknown quantity at this point 
Solved Threads: 1
thenava thenava is offline Offline
Newbie Poster

Re: Else If statement

 
0
  #9
Sep 7th, 2007
or just add

throws IOException

underneath your main method declaration.

...
public static void main(String args[])
throws IOException {
String name;
...


that should work.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

 
1
  #10
Sep 7th, 2007
Originally Posted by thenava View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC