how to write code include a try-catch statement to handle exception of this code. In the catch() statement, will provide a proper alert to the user on the error occurred.

import java.io.*;
import javax.swing.JOptionPane;
class TestArray
{
	public static void main (String []args)
	{
		double a;
		double w;
		double h;
		
		Mammal mamalia[]=new Mammal[2];
		Human people[] = new Human[3];
		
		for(int i=0;i<2;i++)
		{
			a =Double.parseDouble(JOptionPane.showInputDialog(null,"please enter the weight(kg) of mammalia "+(i+1)));
			mamalia[i]=new Mammal(a);
		}
		
		
		System.out.println("properties of mamalia 1 =");
		mamalia[0].display();
		System.out.println("species of mamalia 1  is  ="+mamalia[0].setSpecies("Homo sapiens"));
		System.out.println("properties of mamalia 2  =");
		mamalia[1].display();
		System.out.println("species of  mamalia 2 is = "+mamalia[1].setSpecies("Felis catus"));
		
		for(int i=0;i<3;i++)
		{
			h =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter height(m) of people  "+(i+1)));
			w =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter weight(kg) of people  "+(i+1)));	
			people[i] = new Human(h,w);
		}
		
		String b = JOptionPane.showInputDialog(null,"please enter name of people 1 ");
		people[1].setName(b);
		System.out.println("name of people 1 is "+people[1].setName(b));
		
		
		for(int i=0;i<3;i++)
		{
			people[i].display();
			System.out.println("BMI of people "+(i+1)+(" is ")+people[i].getBMI());
		}
		
	}
}

you can catch array index out of bound exception.

try {
  // your code that could cause an exception to be thrown
} catch (Exception e) { // catch any Exception that may be thrown
  // your code to display an error message
}

you can catch array index out of bound exception.

Never use try-catch for ArrayIndexOutOfBoundsException. You already know at runtime the size or length of what you are trying to use. So always check that.

long89:
So when looping, it is good to do this instead of what you have:

for (int i=0;i<people.length;i++) {
 // use people[i]
}
commented: agreed. avoiding an indexoutofboundsexception is way better than handling it. +14
try {
  // your code that could cause an exception to be thrown
} catch (Exception e) { // catch any Exception that may be thrown
  // your code to display an error message
}
import java.io.*;
import javax.swing.JOptionPane;
class TestArray
{
	public static void main (String []args)
	{
		double a;
		double w;
		double h;
		
		Mammal mamalia[]=new Mammal[2];
		Human people[] = new Human[3];
		
		try
		{
		
		for(int i=0;i<2;i++)
		{
			a =Double.parseDouble(JOptionPane.showInputDialog(null,"please enter the weight(kg) of mammalia "+(i+1)));
			mamalia[i]=new Mammal(a);
		}
		
		for(int i=0;i<3;i++)
		{
			h =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter height(m) of people  "+(i+1)));
			w =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter weight(kg) of people  "+(i+1)));	
			people[i] = new Human(h,w);
		}
		
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null,"wrong entering data types");
		}
		
		
		
		System.out.println("properties of mamalia 1 =");
		mamalia[0].display();
		System.out.println("species of mamalia 1  is  ="+mamalia[0].setSpecies("Homo sapiens"));
		System.out.println("properties of mamalia 2  =");
		mamalia[1].display();
		System.out.println("species of  mamalia 2 is = "+mamalia[1].setSpecies("Felis catus"));
		
		
		String b = JOptionPane.showInputDialog(null,"please enter name of people 1 ");
		people[1].setName(b);
		System.out.println("name of people 1 is "+people[1].setName(b));
		
		
		for(int i=0;i<3;i++)
		{
			people[i].display();
			System.out.println("BMI of people "+(i+1)+(" is ")+people[i].getBMI());
		}
		
	}
}

already solve:D

please makes it as solve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.