Else If statement

Thread Solved

Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Re: Else If statement

 
0
  #11
Sep 7th, 2007
inputs need to be in a try..catch,

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

}

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

Originally Posted by Ezzaral View Post
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.
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
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

 
0
  #12
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.
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!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
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: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

 
0
  #13
Sep 7th, 2007
Originally Posted by rapperhuj View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Re: Else If statement

 
0
  #14
Sep 7th, 2007
^
lol, i guess it's too late...
Last edited by upstream; Sep 7th, 2007 at 5:19 pm.
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,427
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: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Else If statement

 
0
  #15
Sep 7th, 2007
Originally Posted by upstream View Post
^
lol, i guess it's too late...
hehe, yeah I guess so. I had to try though.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
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

 
0
  #16
Sep 8th, 2007
Originally Posted by Ezzaral View Post
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. =)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
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

 
0
  #17
Sep 8th, 2007
@upstream
I tried to use the try..catch, but I think I placed it in the wrong block or something. It has many errors. >.<

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Re: Else If statement

 
0
  #18
Sep 8th, 2007
I've debug it check those comments for the correction of your codes..

  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. }
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
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

 
0
  #19
Sep 8th, 2007
@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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 32
Reputation: upstream is an unknown quantity at this point 
Solved Threads: 1
upstream's Avatar
upstream upstream is offline Offline
Light Poster

Re: Else If statement

 
0
  #20
Sep 8th, 2007
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 ^^
Programming = new Art
---
I <3 BoA Kwon
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC