What wrong in the code~?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: What wrong in the code~?

  1. #1
    Join Date
    May 2003
    Posts
    5

    What wrong in the code~?

    Code:
    import java.io.*;
    
    public class test {
    	public static void main(String[] args){
    		BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
    		String s =  reader.readLine();
    		System.out.println(s);
    
    	}
    }
    What wrong in the code~?

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Firstly, if your code isn't compiling, give us the compiler errors - it means that we don't have to compile the code outselves.

    The compiler error you're getting is:
    Code:
    test.java:6: unreported exception java.io.IOException; must be caught or declared to be thrown
                    String s =  reader.readLine();
                                      ^
    1 error
    I don't know how much Java you know, but the line it's indicating can throw an Exception (IOException, to be precise) - this is Java's error handling mechanism. You therefore either need to declare that your method 'throws' the Exception, or 'catch' the Exception yourself:
    Code:
    import java.io.*;
    
    public class test {
    	public static void main(String[] args) throws IOException {
    		BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
    		String s =  reader.readLine();
    		System.out.println(s);
    
    	}
    }
    or
    Code:
    import java.io.*;
    
    public class test {
    	public static void main(String[] args){
    		try {
    			BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
    			String s =  reader.readLine();
    			System.out.println(s);
    		}
    		catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    If you haven't done exceptions I suggest you read up on them.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    May 2003
    Posts
    5
    ok.....thanks

    I am New to java...and i am chinese..

    my english is so bad that i cant read tutorial on java.sun.com

    sorry!

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    Find a tutorial in Chinese! ;-)
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Sep 2002
    Posts
    124
    Hello, BOY... ni hao !

    You might like to try looking at the following link... http://www.java.com.cn/

    I don't speak Hanyu very much, but it may help.

    Best regards,
    Joyous Monkey

  6. #6
    Join Date
    May 2003
    Posts
    5
    Originally posted by joyous monkey
    Hello, BOY... ni hao !

    You might like to try looking at the following link... http://www.java.com.cn/

    I don't speak Hanyu very much, but it may help.

    Best regards,
    Joyous Monkey
    WELL Are u Chinese~?

    I have been taking a book now~~~

    but also dont know what wrong~~~~~

    haha~

  7. #7
    Join Date
    Mar 2003
    Posts
    84
    k?
    I'm surprised more of you people don't get hit by cars.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center