| | |
Else If statement
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 40
Reputation:
Solved Threads: 0
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:
the Error says that:
illegal start of type line 20
<identifier> expected line 20
Can anyone please tell me how to debug it? =/
Here's my code:
Java Syntax (Toggle Plain Text)
import java.io.*; public class input { public static InputStreamReader Reader=new InputStreamReader (System.in); public static BufferedReader input=new BufferedReader(Reader); public static void main(String args[]); String name; int age; double salary; { System.out.println("Enter name:"); name=input.readline(); System.out.println("Enter age:"); age=Integer.parseInt(input.readLine()); if(age<=18) System.out.println("Too young!"); } else if (age>=18); { System.out.println("You're ready to vote!"); } }
the Error says that:
illegal start of type line 20
<identifier> expected line 20
Java Syntax (Toggle Plain Text)
else if (age>=18);
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.
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.
•
•
Join Date: Apr 2007
Posts: 40
Reputation:
Solved Threads: 0
arg.. a new error occured. >.< this is my only notes/guide for the if else statement. =(
The error says:
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 12
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 14
It seems like my teacher forgot something or I copied the code wrong. =(
Java Syntax (Toggle Plain Text)
import java.io.*; public class input{ public static InputStreamReader Reader=new InputStreamReader(System.in); public static BufferedReader input=new BufferedReader(Reader); public static void main(String args[]){ String name; int age; { System.out.println("Enter name:"); name=input.readLine(); System.out.println("Enter age:"); age=Integer.parseInt(input.readLine()); if (age<=17){ System.out.println("Too young!"); } else if(age>=18){ System.out.println("You're ready to vote!"); } } } }
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 12
Java Syntax (Toggle Plain Text)
name=input.readLine();
declared to be thrown line 14
Java Syntax (Toggle Plain Text)
age=Integer.parseInt(input.readLine());
Last edited by rapperhuj; Sep 6th, 2007 at 10:48 pm.
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
http://java.sun.com/javase/6/docs/ap...l/Scanner.html
•
•
•
•
or just add
throws IOException
underneath your main method declaration.
...
public static void main(String args[])
throws IOException {
String name;
...
that should work.
The catch block should be placed within main() and handle the exception gracefully.
![]() |
Similar Threads
- missing return statement (Java)
- MySQL LIKE statement (MySQL)
- loop in main function to an "if" statement (C++)
- Switch Case Statement (Java)
- run sql statement in asp (ASP)
- switch/case statement (C++)
- change statement (JSP)
- Reading MSWord Document through an ASP Statement (ASP)
Other Threads in the Java Forum
- Previous Thread: Java - to Post on web
- Next Thread: If else statements on compatibility, I'm so confused here?????
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






