iTSCET

iT Portal

A Java program to check the position of number in array using label, break, exception handling !!

import java.util.InputMismatchException;
import java.util.Scanner;
public class BreakWithLabelDemo
{
static int searchfor;

public static void main(String[] args)
{
int[][] arrayOfInts = { { 32, 87, 3, 589 },{ 12, 1076, 2000, 8 },{ 622, 127, 77, 955 }};
System.out.println(“Enter number want to search”);
System.out.println(” Values of Arrays :( { 32, 87, 3, 589 },{ 12, 1076, 2000, 8 },{ 622, 127, 77, 955 }}”);

try
{

Scanner Scan = new Scanner(System.in);
int a= Scan.nextInt();
searchfor=a;
}
catch (InputMismatchException e)
{
System.out.println(“Enter Valid Numbers !!”);
}

int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++)
{
for (j = 0; j < arrayOfInts[i].length; j++)
{
if (arrayOfInts[i][j] == searchfor)
{
foundIt = true;
break search;
}
}
}

if (foundIt)
{
System.out.println("Found, Position of Given number is : " + searchfor +
" at " + i + ", " + j);
}
else
{
System.out.println(searchfor+ " not in the array");
}
}

}

by Ashokkumar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: