Calculate Rectangle Area - Java Language Basics

Java examples for Language Basics:int

Introduction

Area of a rectangle is length * width

Demo Code


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

  public static void main(String[] args) {

    int width = 0;
    int length = 0;

    try {//from   w w w.j a v  a2 s  .  com
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter length of a rectangle");
      length = Integer.parseInt(br.readLine());
      System.out.println("Please enter width of a rectangle");
      width = Integer.parseInt(br.readLine());
    }
    catch (NumberFormatException ne) {
      System.out.println("Invalid value" + ne);
      System.exit(0);
    } catch (IOException ioe) {
      System.out.println("IO Error :" + ioe);
      System.exit(0);
    }

    int area = length * width;

    System.out.println("Area of a rectangle is " + area);
  }

}

Result


Related Tutorials