In: Computer Science
package com.demo.qa.Dto;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class MedianDemo {
public static void findMedian(ArrayList a, int n)
{
Collections.sort(a);
if(n % 2 != 0)
{
System.out.println("Median : "+ a.get(n/2));
}
int n1 = (int) a.get((n-1)/2);
int n2 = (int) a.get((n)/2);
System.out.println("Median :"+(n1+n2)/2.0);
}
public static void main(String args[]){
for(int i=0;i< 1;++i)
{
try
{
File file=new File("F:/ecllipse projects/demofile.txt");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
StringBuffer sb=new StringBuffer();
String line;
while((line=br.readLine())!=null)
{
String str[] = line.split(",");
ArrayList<Integer> inc = new ArrayList<Integer>();
for(int j=0;j< str.length;++j)
{
String ss =str[j];
if(ss == null || ss.equals("")==true || ss.contains(" ") == true || ss.contains("\n") == true)
{
continue;
}
else
{
int q = Integer.parseInt(ss);
inc.add(q);
}
}
System.out.println("List size : " + inc.size());
if(inc.size() != 0)
{
findMedian(inc,inc.size());
}
}
fr.close();
System.out.println("Contents of File: ");
System.out.println("\n\n\n\n");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Not open");
}
}
}
}