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? =/

Your braces and semicolons are out of place. Look through your if () else if () block and check them.

uhmmm.. I'm sorry, but I already tried to move the braces at the "else if" line. Still I can't let it run. =(

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.

ok.. thx.. I'll check them.. =)

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. =(

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

or just add

throws IOException

underneath your main method declaration.

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


that should work.

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.

commented: Non sugarcoated straight talking advice! +11

inputs need to be in a try..catch,

format:
try {
statement
}
catch(Exception ex) {
statement

}

ex...

try {
    System.out.println("Enter name:");
    name=input.readLine();
}
catch(IOException ioe) {}

my teacher doesn't teach me that too <.<...

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/api/java/util/Scanner.html

yup, java 6's scanner is way too much better :)

or just add

throws IOException

underneath your main method declaration.

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


that should work.

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!

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.

^
lol, i guess it's too late...

^
lol, i guess it's too late...

hehe, yeah I guess so. I had to try though.

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.

Sorry, but I don't know what to do with try and catch. xD I'm still a noob. >.< We were not taught how to use that yet. =/

but I'll try it. =)

@upstream
I tried to use the try..catch, but I think I placed it in the wrong block or something. It has many errors. >.<

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;
    try{
    System.out.println("Enter name:");
    name=input.readLine();
    }
    catch(IOException ioe) {}
    try{
    System.out.println("Enter age:");
    age=Integer.parseInt(input.readLine());
    }
    catch (IOException ioe) {}
    if (age<=17){
    System.out.println("Too Young");
    }else if(age>=18){
    System.out.println("You're ready to vote!");
    }
}

I've debug it :) check those comments for the correction of your codes..

import java.io.*;
public class Input
{
//    public static InputStreamReader reader=new InputStreamReader(System.in);
//    public static BufferedReader input=new BufferedReader(Reader);
	//Wrong declaration of buffered reader (I think...) use this instead
	public static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	
    public static void main (String args[]) //Your void main has a ";" which only use to terminate a statement
	{ //You missed the opening bracket of main 
	    String name;
	    int age=0; //Variables needs to initialize first
	    try{ //If you have multiple input you can use one try...catch statement to all like input like this...
	    System.out.println("Enter name:");
	    name=input.readLine();
	    System.out.println("Enter age:");
	    age=Integer.parseInt(input.readLine());
	    }
	    catch(IOException ioe) {}
	
	    if (age<=17){
	    System.out.println("Too Young");
	    }else if(age>=18){
	    System.out.println("You're ready to vote!");
		}
	} // You also missed the closing bracket of the main
}

@upstream
wow.. thx upstream! =) It will be a big help for me, coz 1 week more and it will be our prefinals. xD so we have 2 meetings more left and in that time we will be taking up the "Switch Structure". xD btw, I'm also from ph. =) thx again!

cool, i suggest you do advance reading. so you can learn the basic structures and rules in java programming, we can't really rely much on our instructors cuz they dont care much about the important facts in programming, they just dump the code right into us (not to mention, most teacher only knows theory :p)...and explain less. im a senior college student too in the philippines, so i know that...goodluck in your studies ^^

cool, i suggest you do advance reading. so you can learn the basic structures and rules in java programming, we can't really rely much on our instructors cuz they dont care much about the important facts in programming, they just dump the code right into us (not to mention, most teacher only knows theory :p)...and explain less. im a senior college student too in the philippines, so i know that...goodluck in your studies ^^

thx for the tip! I'll do that. =)

try to run this coding it will run
import java.io.*;
class input
{
public static void main( String args[])
{
String name ;
int age;
int salary;
int a2;
String s1,a1;
BufferedReader br = new BufferedReader ( new InputStreamReader (System.in));
System.out.println( " enter your name " );
s1 = br.readLine();
System.out.println (" enter your age ");
a1 = br.readLine();
a2 = Integer.parseInt(a1);
if ( a2 < 18)
{
System.out.println ( " you are not eligble for vote");
} //System.out.println (s1);



catch(IOException ioe) {}



}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.