| | |
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?????
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint 2dgraphics android api apple applet application applications arguments array arrays automation bank bidirectional binary bluetooth chat class classes client code collision component database db development draw eclipse eclipsedevelopment error event exception fractal game givemetehcodez graphics gui homework html ide image input integer integration j2me jarfile java javadesktopapplications javafx javaprojects jmf jni jpanel julia learningresources linux list loop map method methods mobile mysql netbeans newbie number oracle print problem producer program programming project projectideas recursion researchinmotion scanner screen server service set size sms socket sort sorting sql sqlserver state string swing swt tcp test text-file threads time tree web windows






