943,572 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 8048
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 7th, 2007
0

Re: Else If statement

inputs need to be in a try..catch,

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

}

ex...
Java Syntax (Toggle Plain Text)
  1. try {
  2. System.out.println("Enter name:");
  3. name=input.readLine();
  4. }
  5. catch(IOException ioe) {}

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

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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
yup, java 6's scanner is way too much better
Last edited by upstream; Sep 7th, 2007 at 4:14 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 2007
Sep 7th, 2007
0

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.
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!
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

Click to Expand / Collapse  Quote originally posted by rapperhuj ...
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.
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

^
lol, i guess it's too late...
Last edited by upstream; Sep 7th, 2007 at 5:19 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 2007
Sep 7th, 2007
0

Re: Else If statement

Click to Expand / Collapse  Quote originally posted by upstream ...
^
lol, i guess it's too late...
hehe, yeah I guess so. I had to try though.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Sep 8th, 2007
0

Re: Else If statement

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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. =)
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 8th, 2007
0

Re: Else If statement

@upstream
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)
  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. try{
  11. System.out.println("Enter name:");
  12. name=input.readLine();
  13. }
  14. catch(IOException ioe) {}
  15. try{
  16. System.out.println("Enter age:");
  17. age=Integer.parseInt(input.readLine());
  18. }
  19. catch (IOException ioe) {}
  20. if (age<=17){
  21. System.out.println("Too Young");
  22. }else if(age>=18){
  23. System.out.println("You're ready to vote!");
  24. }
  25. }
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 8th, 2007
0

Re: Else If statement

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

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. public class Input
  3. {
  4. // public static InputStreamReader reader=new InputStreamReader(System.in);
  5. // public static BufferedReader input=new BufferedReader(Reader);
  6. //Wrong declaration of buffered reader (I think...) use this instead
  7. public static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  8.  
  9. public static void main (String args[]) //Your void main has a ";" which only use to terminate a statement
  10. { //You missed the opening bracket of main
  11. String name;
  12. int age=0; //Variables needs to initialize first
  13. try{ //If you have multiple input you can use one try...catch statement to all like input like this...
  14. System.out.println("Enter name:");
  15. name=input.readLine();
  16. System.out.println("Enter age:");
  17. age=Integer.parseInt(input.readLine());
  18. }
  19. catch(IOException ioe) {}
  20.  
  21. if (age<=17){
  22. System.out.println("Too Young");
  23. }else if(age>=18){
  24. System.out.println("You're ready to vote!");
  25. }
  26. } // You also missed the closing bracket of the main
  27. }
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 2007
Sep 8th, 2007
0

Re: Else If statement

@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!
Last edited by rapperhuj; Sep 8th, 2007 at 6:35 am.
Reputation Points: 7
Solved Threads: 0
Light Poster
rapperhuj is offline Offline
40 posts
since Apr 2007
Sep 8th, 2007
0

Re: Else If statement

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 ^^
Reputation Points: 10
Solved Threads: 1
Light Poster
upstream is offline Offline
43 posts
since Sep 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