Results 1 to 10 of 10

Thread: Throwing Exceptions

Threaded View

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved Throwing Exceptions

    Can i use the throws clause on a static method?

    Here is my code, it has an error in it.
    Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    /**
     * Store items
     * 
     * @author Daniel Morgan [362724]
     * @version 1.0
     */
    
    public class Library
    {
        private static ArrayList theItems;
    
        /**
         * Create a library
         */
        public Library()
        {
            //ArrayList that will store all the new items
            theItems = new ArrayList();
        }
        
        /**
         * Run from command line
         */
        public static void main()
        {
            //Create a new library
            Library newLib = new Library();
            //Start command interface
            retrieveInput();
        }
    
        /**
         * Create a new item and add it to the library arraylist
         * 
         * @param Name The name of the item
         * @param cost The cost of the item
         */
        public static void createItem(String Name, int cost)
        {
            //Create a new instance of an item
            Item newItem = new Item(Name, cost);      
            //Add it to the array list
            theItems.add(newItem);
        }
        
        /**
         * Print all items in the library
         */
        public static void printAll()
        {
            //Create an iterator
            Iterator itCount = theItems.iterator();
            //Loop through all items in the library
            while(itCount.hasNext())
            {
                Item currentItem = (Item)itCount.next();
                //Print the item details
                currentItem.print();
            }        
        }
        
        
        /**
         * Retrieve the user input and provide the command interface
         * 
         * @throws IOException
         */    
        public static void retrieveInput() throws IOException
        {
            //The input reader
    	    BufferedReader usrInput = new BufferedReader(new InputStreamReader(System.in));
    	    //'Name' and 'Cost' variables
    	    String itemName = "";
    	    String inputAnswer= "";
    	    int itemCost = 0;
    	    Boolean contInput = true;
    	    
    	    while(contInput = true)
    	    {
        	    //Prompt user to create a new item
        	    System.out.println("Do you want to create an item? (Yes or No)"); System.out.println("> ");
        	    //Read input
        	    inputAnswer = usrInput.readLine();
        	    //Check input
        	    if(inputAnswer.toLowerCase().equals("yes"))
        	    {
        	        //Prompt user for item 'Name'
        	        System.out.println("Item name?"); System.out.println("> ");
        	        //Read input
        	        itemName = usrInput.readLine();
        	        //Prompt user for item 'Cost'
        	        System.out.println("Item Cost?"); System.out.println("> ");
        	        itemCost = Integer.parseInt(usrInput.readLine());
        	        //Create the item
        	        createItem(itemName, itemCost);
        	        //Notify user of success
        	        System.out.println("Item created successfully");
        	        //Print a blank line, for presentation purposes
        	        System.out.println();
        	    }//End If
        	    	    
        	    //Prompt the user to print all items
        	    System.out.println("Do you want to print all items? (Yes or No)"); System.out.println("> ");
        	    //Read input
        	    inputAnswer = usrInput.readLine();
        	    //Check input
        	    if(inputAnswer.toLowerCase().equals("yes"))
        	    {
        	        //Print all items
        	        printAll();
        	        //Print a blank line, for presentation purposes
        	        System.out.println();
        	    }//End If
        	    
        	    //Repeater
        	    System.out.println("Repeat this process? (Yes or No)"); System.out.println("> ");
        	    //Read input
        	    inputAnswer = usrInput.readLine();
        	    //Check input
        	    if(inputAnswer.toLowerCase().equals("yes"))
        	    {
        	        //Set continue flag
        	        contInput = true;
        	    }
        	    else
        	    {
        	        //Set continue flag
        	        contInput = false;
        	    }//End If
    	    }//End While
        }
        
    }
    The error is in the line highlighted in bold. The error is:

    Unreported exception java.io.IOException; must be caught or declared to be thrown.
    Last edited by x-ice; Feb 25th, 2007 at 07:21 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width