Friday, July 6, 2018

Solution one for 12 to 2 batch : 06 - JULY - 2018

This code is implemented and provided by Vaishnavi Srivastava from UCER, Allahabad, UP, India

import java.io.FileInputStream;
import java.util.Scanner;

public class TheFIS01 
{
public static void main(String[] args)
{
try
{
// variables to keep the count of new line,upper case 
                       // and lower case alphabets,numeric values,
//spaces and vowels
int nl=0,uca=0,lca=0,nv=0,sp=0,v=0;

//create object of the scanner class to take the input from the user
Scanner sc=new Scanner(System.in);

//input the path 
System.out.println("Input A Path:");
String path=sc.nextLine();

System.out.println("#REPORT#");

//open the file in read mode
FileInputStream fi=new FileInputStream(path);

//get the no. of bytes those can be read by the read() method
int nob=fi.available();

//create an array of byte to store the data of the file
byte[] array=new byte[nob];

//fetch file data and store it inside the array
fi.read(array);

//start a for loop so as to check the type of element and get its count 
for (int i = 0; i < array.length; i++) 
{
//ASCII value of new line is 10
if(array[i]==10)
{
//the variable will keep the count of any new line
nl++;
}

//ASCII values of upper case alphabets are from 65-90
if(array[i]>=65&&array[i]<=90)
{
//the variable will keep the count of all the upper case 
uca++;
}

//ASCII values of lower case characters are from 97-122
if(array[i]>=97&&array[i]<=122)
{
//the variable will keep the count of all the lower case 
lca++;
}

//ASCII values of 0-9 is 48-57
if(array[i]>=48&&array[i]<=57)
{
//the variable will keep the count of the numeric values
nv++;
}

//ASCII value of space is 32
if(array[i]==32)
{
//this variable will keep the count of all the spaces
sp++;
}

//ASCII values of all the vowels be it in upper case or lower case
if(array[i]==65||array[i]==97||array[i]==70||array[i]==102||array[i]==74||array[i]==106||array[i]==80||array[i]==112||array[i]==86||array[i]==118)
{
//this variable will keep the count of the vowels
v++;
}
}

//total alphabets calculated
                       // (upper case alphabets+lower case alphabets)
int ta=lca+uca;

//convert array of byte iinto string
String str=new String(array);

//print everything
System.out.print(str);
System.out.println("Size Of Data:"+nob+"bytes");
System.out.println("No. of lines:"+nl);
System.out.println("Total Alphabets:"+ta);
System.out.println("No. of Upper Case Alphabets:"+uca);
System.out.println("No. of Lower Case Alphabets:"+lca);
System.out.println("Total no. of numeric values:"+nv);
System.out.println("Total no. of spaces:"+sp);
System.out.println("Total no. of vowels:"+v);
System.out.println("#END OF REPORT");

//close the file

fi.close();
}
               
catch(Exception e)
{
System.out.println("Error:"+e);
}

}
}

No comments:

Post a Comment