RSS Forums RSS

Else If statement

Please support our Java advertiser: Programming Forums
Thread Solved
Reply
Posts: 40
Reputation: rapperhuj is an unknown quantity at this point 
Solved Threads: 0
rapperhuj rapperhuj is offline Offline
Light Poster

Help Else If statement

  #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:

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

else if (age>=18);

Can anyone please tell me how to debug it? =/
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,111
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: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

  #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  
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

  #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  
Posts: 4,111
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: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

  #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  
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

  #5  
Sep 6th, 2007
ok.. thx.. I'll check them.. =)
Reply With Quote  
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

  #6  
Sep 6th, 2007
arg.. a new error occured. >.< this is my only notes/guide for the if else statement. =(
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!");
    }
    }
    }
}

The error says:
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 12
name=input.readLine();
- unreported exception java.io.IOException; must be caught or
declared to be thrown line 14
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 9:48 pm.
Reply With Quote  
Posts: 3,465
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Else If statement

  #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, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote  
Posts: 4,111
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: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

  #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  
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

  #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  
Posts: 4,111
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: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 4611 | Replies: 21 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:31 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC