Results 1 to 4 of 4
  1. #1
    learning_java is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default Unreported exception java.io.IOException; must be caught or declared to be thrown

    Thank you for taking the time to help. I really appreciate it.

    I am getting the above error and am not sure which direction to proceed. In the class, the only way we have discussed getting user input is by System.in.read. I have searched and apparently found better methods for getting user input, but wanted to stick with what has been presented thus far.

    Assignment Directions:
    1. Create a new class named “valuemethod”
    2. Create a new method named “Main”
    3. In Main Write the code that will call the method EnterPay and YearlySal
    4. Create a new method named “EnterPay”
    5. In the EnterPay method Write the code that asks the user to input their hourly wage. Use the formula to calculate their yearly salary: wage * 2040. Return the yearly salary to the main method
    6. In the main method write the code that will display this: “Your yearly salary is:



    Java Code:
    package valuemethod;
    
    public class Valuemethod 
    {
    
        public static void main(String[] args) 
        
        	//throws java.io.IOException -- moved to EnterPay
        {
            //double HourlyWage = EnterPay();
    	double YearlySal = EnterPay();	
    	System.out.println ("Your yearly salary is: $" + YearlySal + "." );
        }
            
    	public static double EnterPay()
            throws java.io.IOException
                {
    		double HourlyWage = 0;
                    double YearlySal = 0;
                    
                            {
                                System.out.print("Enter your hourly wage:");
                                       HourlyWage = (double) System.in.read();
                            }
                            YearlySal = HourlyWage*2040;
                            return YearlySal;
                }
    		 
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Unreported exception java.io.IOException; must be caught or declared to be thrown

    Your method is declared to throw an IOException which is a checked exception. So you
    need a try catch block. Exceptions and their handling are important so read about them
    here -> Lesson: Exceptions (The Java Tutorials > Essential Classes)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Unreported exception java.io.IOException; must be caught or declared to be thrown

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    learning_java is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default Re: Unreported exception java.io.IOException; must be caught or declared to be thrown

    Thanks Jim and Darryl, I really appreciate the advice.

Similar Threads

  1. Replies: 5
    Last Post: 05-11-2014, 09:42 PM
  2. Replies: 1
    Last Post: 11-03-2012, 02:42 PM
  3. Replies: 2
    Last Post: 04-27-2012, 03:43 PM
  4. Replies: 7
    Last Post: 03-21-2012, 09:07 AM
  5. Replies: 5
    Last Post: 02-15-2011, 12:39 AM

Posting Permissions

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