| | |
Else If statement
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
inputs need to be in a try..catch,
format:
try {
statement
}
catch(Exception ex) {
statement
}
ex...
my teacher doesn't teach me that too <.<...
yup, java 6's scanner is way too much better
format:
try {
statement
}
catch(Exception ex) {
statement
}
ex...
Java Syntax (Toggle Plain Text)
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/ap...l/Scanner.html
Last edited by upstream; Sep 7th, 2007 at 4:14 pm.
•
•
Join Date: Apr 2007
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
or just add
throws IOException
underneath your main method declaration.
...
public static void main(String args[])
throws IOException {
String name;
...
that should work.
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!
Add a try{} catch () {} block around the code that is using the reader as other posters have indicated.
•
•
Join Date: Apr 2007
Posts: 40
Reputation:
Solved Threads: 0
•
•
•
•
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.
but I'll try it. =)
•
•
Join Date: Apr 2007
Posts: 40
Reputation:
Solved Threads: 0
@upstream
I tried to use the try..catch, but I think I placed it in the wrong block or something. It has many errors. >.<
I tried to use the try..catch, but I think I placed it in the wrong block or something. It has many errors. >.<
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; 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..
check those comments for the correction of your codes.. 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); //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 }
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 ^^
![]() |
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?????
Views: 5648 | Replies: 21
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






