For some reason, I\'m getting erorr message everytime I run the code saying that
ID: 3797231 • Letter: F
Question
For some reason, I'm getting erorr message everytime I run the code saying that it cannot locate the csv file or
Exception occured: java.io.FileNotFoundException: bank-Details.csv (No such file or directory)
I have added the csv file to the directory however it still fails to locate it , here is the code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class BankRecords extends Client{
FileReader fr = new FileReader("bank-Detail.csv"); // REading File bank-Details from D: Drive
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
ArrayList<String> dataList = new ArrayList<String>();
BankRecords bankrecords[];
BankRecords(){
}
@Override
void readData() {
try{
//br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
dataList.add(line);
}
bankrecords= new BankRecords[dataList.size()];
}
catch(FileNotFoundException e){
System.out.println("Exception occured: "+e);
}
catch(Exception e){
System.out.println("Exception occured: "+e);
}finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
void processData() {
// TODO Auto-generated method stub
for(int i=0;i<dataList.size();i++){
String data[] = dataList.get(i).split(",");
bankrecords[i] = new BankRecords();
bankrecords[i].setId(data[0]);
bankrecords[i].setAge(Integer.parseInt(data[1]));
bankrecords[i].setSex(data[2]);
if(data[3].equals("TOWN") || data[3].equals("RURAL") )
bankrecords[i].setRegion(data[3]+" ");// for proper formatting of the output i have added these extra spaces
else
bankrecords[i].setRegion(data[3]);
bankrecords[i].setIncome(Float.parseFloat(data[4]));
if("YES".equals(data[5]) || "yes".equals(data[5]))
bankrecords[i].setMarried(true);
else
bankrecords[i].setMarried(false);
bankrecords[i].setChildern(Integer.parseInt(data[6]));
if("YES".equals(data[7]) || "yes".equals(data[7]))
bankrecords[i].setHasCar(true);
else
bankrecords[i].setHasCar(false);
if("YES".equals(data[8]) || "yes".equals(data[8]))
bankrecords[i].setHasSav_act(true);
else
bankrecords[i].setHasSav_act(false);
if("YES".equals(data[9]) || "yes".equals(data[9]))
bankrecords[i].setHasCur_act(true);
else
bankrecords[i].setHasCur_act(true);
if("YES".equals(data[10]) || "yes".equals(data[10]))
bankrecords[i].setMortgage(true);
else
bankrecords[i].setMortgage(false);
if("YES".equals(data[10]) || "yes".equals(data[10]))
bankrecords[i].setPep(true);
else
bankrecords[i].setPep(false);
}
}
@Override
void PrintData() {
// TODO Auto-generated method stub
//System.out.printf("%-20s%-11s%-18s%-16s%-12s%-26s%-12s ","S.no","ID","AGE","SEX","REGION","INCOME","MORTGAGE");
System.out.println("S.no "+" ID "+"AGE "+ "SEX "+"REGION "+ "INCOME " +"MORTGAGE ");
for(int i=0;i<25;i++){
System.out.println( (i+1)+" "+bankrecords[i].getId()+" "+bankrecords[i].getAge()+" "+bankrecords[i].getSex()+" "+bankrecords[i].getRegion()+" "+bankrecords[i].getIncome()+" "+bankrecords[i].isMortgage());
}
}
public static void main(String args[]){
BankRecords bankrecord = new BankRecords();
bankrecord.readData();
bankrecord.processData();
bankrecord.PrintData();
}
}
Explanation / Answer
Please follow the data and description :
FileNotFoundException :
In general a FileNotFoundException is thrown in the following cases :
a) If the program used to search for a file claims that there is no such file or directory, then the user must verify that the specified line of code to read/write the data is correct and that actually points to a file or directory that exists in the system where the code runs.
b) Another case is that if a message claims that the permission to access/read/write/open is denied then, the user must first check if the permissions of the file are correct that matches the requriement and secondly is to check, if the file is currently being used by another application.
c) Lastly this is also raised if the message claims that the specified file is a directory, then the user should either alter the name of the file or should delete the existing directory.
I suppose the code has a glitch as the file even placed in the same directory as of the program that is being used, then the file needs to be specified correctly.
a) Check for the file name if correct.
b) The code specifies that the file is read using the file reader but that the bufferred reader is left with a null initialisation. This should be updated by BufferedReader br = new BufferedReader(new FileReader(file));
The file here also need ot be in the same directory, if at all to avoid confusion we could provide the whole path of the file in the above line.
Hope this is helpful.