943,608 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 8048
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 6th, 2007
0

Else If statement

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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

Java Syntax (Toggle Plain Text)
  1. else if (age>=18);
Can anyone please tell me how to debug it? =/
Similar Threads
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 6th, 2007
0

Re: Else If statement

Your braces and semicolons are out of place. Look through your if () else if () block and check them.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Sep 6th, 2007
0

Re: Else If statement

uhmmm.. I'm sorry, but I already tried to move the braces at the "else if" line. Still I can't let it run. =(
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 6th, 2007
0

Re: Else If statement

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Sep 6th, 2007
0

Re: Else If statement

ok.. thx.. I'll check them.. =)
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 6th, 2007
0

Re: Else If statement

arg.. a new error occured. >.< this is my only notes/guide for the if else statement. =(
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  1. name=input.readLine();
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 14
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 7th, 2007
0

Re: Else If statement

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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Sep 7th, 2007
0

Re: Else If statement

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Sep 7th, 2007
0

Re: Else If statement

or just add

throws IOException

underneath your main method declaration.

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


that should work.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thenava is offline Offline
6 posts
since Sep 2007
Sep 7th, 2007
1

Re: Else If statement

Click to Expand / Collapse  Quote originally posted by thenava ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java - to Post on web
Next Thread in Java Forum Timeline: If else statements on compatibility, I'm so confused here?????





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC