-3

I have been new to Java. Today, I am doing project on java on BMI calculation.

My code so far is:

package extra_programs;
import java.io.*;


public class bmiCalculation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter your weight in kgs");
          int  wt=Integer.parseInt( reader.readLine());
          System.out.println("Enter your weight in cms");
          double height=Double.parseDouble(reader.readLine());
          height=height/100;
          double bmi=wt/(height*height);

          if(bmi<18.5){
              System.out.println("You are underweight");
          }
          else if(bmi>=18.5 && bmi<25){
              System.out.println("You are The right weight");
          }
          else if(bmi>=25 && bmi<30){
              System.out.println("You are overweight,Plz start jogging");
          }
          else if(bmi>=30){
              System.out.println("You are obese,Plz start jogging and tryt to reduce weight.");
          }
          else{
              System.out.println("Not a valid bmi");
          }



    }

}

Whenever I compline the program, error pops up as:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Unhandled exception type IOException
    Unhandled exception type IOException

    at extra_programs.bmiCalculation.main(bmiCalculation.java:13)

Please help me how to solve this problem.

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
pallavidestiny22
  • 335
  • 1
  • 8
  • 19
  • Is there a specific reason behind using BufferedReader?? If not use Scanner instead of BufferedReader to read values. – ganeshvjy Jun 27 '15 at 18:47

2 Answers2

2

Modify your main by adding a throws declaration

 public static void main(String[] args) throws  IOException {

as reader.readLine() may throw an unhandled exception.

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
1

You should surround your code with a try/catch block which will catch potential IOExceptions.

Like this:

public class bmiCalculation {

    public static void main(String[] args) {
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter your weight in kgs");
      try {
           int wt=Integer.parseInt(reader.readLine());
           System.out.println("Enter your weight in cms");
           double height=Double.parseDouble(reader.readLine());
           height=height/100;
           double bmi=wt/(height*height);
           if(bmi<18.5) {
             System.out.println("You are underweight");
           }
           else if(bmi>=18.5 && bmi<25) {
             System.out.println("You are The right weight");
           }
           else if(bmi>=25 && bmi<30) {
             System.out.println("You are overweight,Plz start jogging");
           }
           else if(bmi>=30) {
             System.out.println("You are obese,Plz start jogging and try to reduce weight.");
           }
           else {
             System.out.println("Not a valid bmi");
           }
      }
      catch(IOException e) {
             System.out.println("Exception caught: " + e.printStackTrace());
      }
   }           
}
smoggers
  • 3,154
  • 6
  • 26
  • 38