Jump to content
  • Advertisement
Sign in to follow this  
Carbon101

Java input questions

This topic is 2129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am reviewing my Java notes and I stumbled into a question. What is the difference between throws java.io.IOException and the Scanner class. Which one should be used when? Thanks in advance. Feel free to ask more questions for more clarification.

Share this post


Link to post
Share on other sites
Advertisement

The one you have provided is a throw clause or a throw declaration(Eclipse IDE calls it a throw declaration) to handle exceptions that is directly related to input and output. That is what IO means Input and Output. The IOException is commonly used when you want to type code to read an image from a directory. You can either use the throw clause to handle the Input and Output exception or you can use the try and catch block below. You will notice the IOException in the catch block.

 

Generally the try and catch block is used more than the throw clause because you would only need it in the code that takes in input from the user or from a directory. Using a throw clause can be messy code when you need to use a throw clause in the constructor of a class that has the input and output code. The result would be having to use throw clauses in many areas of a different class(this would be the class where the instance of the class that has the input and out code). So sticking to a try and catch block leads to more elegant code.

 

The code below reads an image from a directory. If the directory is wrong, the catch block will be executed. If the directory is correct, you won't see the exception generated in the console. In either case where the directory was correct or wrong, the try block will always execute.

 

Example:

try
        {
                image = ImageIO.read(new File("src/Bat/Bat0.PNG"));
               
                
                
        }
        catch (IOException e) {
                e.printStackTrace();
        }

Tons of information about exceptions and answers to your future questions about it:

http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

 

Scanner is something different. Scanner takes an input from the console, where the user type their input. When you create an object out of the Scanner class, that scanner object takes in an input from the user. You can tell the scanner object what type of input you want it to take in depending on what method call you directed to the scanner object.

 

The code below scanner takes in a string input from the user.

 

Example:

Scanner scanner = new Scanner();
String stringInput = scanner.next();
System.out.println(stringInput);

Scanner can also throw different exceptions depending on which Scanner object you are using:

http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html

 

Example:

 

Scanner that takes in a file object as its parameter throws a FileNotFoundException.

 

Scanner that takes in a input stream and string object as its parameters throws a IllegalArgumentException.

Edited by warnexus

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!