Skip to main content

Not reading my file. through exception

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
gladi
Offline
Joined: 2014-06-08

<
package sd_2;

public class SD_2 {

public static void main(String[] args) {
// TODO Auto-generated method stub
LOC loc = new LOC();
loc.ReadFile();
loc.Print();

}
}

>
<
package sd_2;

public class FunctionData {

private int numLOC;
private String funName;

public int getNumLOC() {
return numLOC;
}

public void setNumLOC(int numLOC) {
this.numLOC = numLOC;
}

public String getFunName() {
return funName;
}

public void setFunName(String funName) {
this.funName = funName;
}

}

>

<

package sd_2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.sound.sampled.LineUnavailableException;

public class LOC {
private ArrayList funDatas;
public LOC()
{
funDatas = new ArrayList();
isMatchFunction = false;
isStartFunction = false;
}
private boolean isMatchFunction;
private boolean isStartFunction;
public void ReadFile()
{
Scanner input = new Scanner(System.in);
System.out.print("Input file Name: ");

String fileName = input.nextLine();
String functionName="";
BufferedReader br = null;

try {

String sCurrentLine;

br = new BufferedReader(new FileReader(fileName));

while ((sCurrentLine = br.readLine()) != null) {
int a = ProcessLine(sCurrentLine);
if(a == -1)
{
while(a != 1)
{
sCurrentLine = br.readLine();
a = ProcessLine(sCurrentLine);
}
}

if(a == 3)
{
functionName = sCurrentLine;
isMatchFunction = true;
}
else
{
isMatchFunction = false;
continue;
}

if(isMatchFunction == true)
{
if(a == 4)
{
isStartFunction = true;
}
else
{
functionName = "";
isStartFunction = false;
continue;
}

}
if(isStartFunction == true)
{
String newLine;
newLine = br.readLine();
int b = ProcessLine(newLine);
FunctionData fData=new FunctionData();
fData.setFunName(functionName);
int count=0;
while(b!=5)
{
if(b > 2)
{
count++;
}
if(b == -1)
{
while(b!=1)
{
newLine = br.readLine();
b = ProcessLine(newLine);
}
}
newLine = br.readLine();
b = ProcessLine(newLine);
}

fData.setNumLOC(count);
funDatas.add(fData);
isMatchFunction = false;
isStartFunction = false;
}

}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}

public void Print()
{
for(int i= 0; i < funDatas.size(); i++)
{
System.out.println(funDatas.get(i).getFunName());
System.out.println(funDatas.get(i).getNumLOC());
}

}
public int ProcessLine(String line)
{
if(line.startsWith("/*"))
return -1;
if(line.startsWith("//"))
return 0;
if(line.endsWith("*/"))
return 1;

if(line.trim().equals(""))
return 2;
String a = line.trim();
a = a.replace("\\t", "");
if(a.trim().equals(""))
return 2;
String b[] = line.split(" ");
if(b.length >= 2 && line.contains("(") && line.contains(")"))
return 3;
if(line.contains("{"))
return 4;
if(line.contains("}"))
return 5;
return 6;
}
}

>