Monday, March 12, 2012

OccurancesInArray

import java.io.*;

public class OccurancesInArray{
  public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("How many elements you want to enter in the array: ");
  int num=0;
  try{
  num = Integer.parseInt(in.readLine());
  }
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() " is not a number!");
  System.exit(0);
  }
  String[] elements = new String[num];
  int a;
  int k;
  for(int i = 0; i < num; i++){
  elements[i= in.readLine();
  }
  for(int i = 0; i < elements.length; i++){
  a = 0;
  k = 1;
  for(int j = 0; j < elements.length; j++){
  if(j >= i){
  if(elements[i].equals(elements[j]) && j != i){
  k++;
  }
  }
  else if(elements[i].equals(elements[j])){
  a = 1;
  }
  }
  if(a != 1){
  System.out.println("Occurance of \'" + elements[i"\' : " + k);
  }
  }
  }
}

No comments:

Post a Comment