[ stemleaf.java ]
import java.io.*;class stemleaf
{
public static void main(String args[]) throws IOException
{
int i=0;
int no[]=new int[100];
String line;
File f=new File(args[0]);
/*
// WRITING DATA TO FILE
System.out.println("WRITING DATA TO FILE");
BufferedWriter br=new BufferedWriter(new FileWriter(f));
while(i<10)
{
line=""+i;
br.write(line,0,line.length());
br.newLine();
i++;
}
br.close();
*/
System.out.println("READING FROM FILE");
BufferedReader bf=new BufferedReader(new FileReader(f));
int j=0,temp;
while( (line=bf.readLine())!=null)
{
temp=Integer.parseInt(line);
if(temp<1000 && temp>=10)
{
no[j]=Integer.parseInt(line);
j++;
}
}
int leaf,stem;
sort(no,j);
System.out.println("===========================");
System.out.println("STEM \t | \t LEAF");
System.out.println("===========================");
for(i=0; i<j;)
{
stem=no[i]/10;
System.out.print(stem + "\t | \t" );
while(no[i]/10==stem)
{
leaf=no[i]%10;
System.out.print(leaf);
if (no[i+1]/10==stem)
{
System.out.print(",");
}
i++;
}
System.out.println();
}
System.out.println("===========================");
}
static void sort(int arr[],int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i]<arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
}
No comments:
Post a Comment