Question: I'm having problems getting the program to read the file I have in the src folder in the project. When I run the program I

I'm having problems getting the program to read the file I have in the src folder in the project. When I run the program I get nothing on the output and program just terminates. I moved the txt file to just under the project folder and I get unable to read file. Can anyone help me figure out whats going on.

MyDate Class:

public class MyDate implements Comparable {

//Private Variables

private int month;

private int day;

private int year;

//MyDate Constructor

public MyDate(int month, int day, int year) {

this.month = month;

this.day = day;

this.year = year;

}

//Compare MyDate

@Override

public int compareTo(MyDate obj) {

if (this.year > obj.year) {

return 1;

}

if (this.year < obj.year) {

return -1;

}

if (this.year == obj.year) {

if (this.month > obj.month) {

return 1;

}

if (this.month < obj.month) {

return -1;

}

if (this.month == obj.month) {

if (this.day > obj.day) {

return 1;

}

if (this.day < obj.day) {

return -1;

}

}

}

return 0;

}

//MyDate results as String

@Override

public String toString() {

return "MyDate [month=" + month + ", day=" + day + ", year=" + year + "]";

}

}

Person Class:

public class Person {

//Private Variables

private String name;

private MyDate birthday;

public Person(String name, MyDate birthday) {

this.name = name;

this.birthday = birthday;

}

//Compare Person

public int compareTo(Person obj) {

int var = this.birthday.compareTo(obj.birthday);

if (var == 0) {

return this.name.compareTo(obj.name);

}

else

return var;

}

//Person results as String

@Override

public String toString() {

return "Person [name=" + name + ", birthday=" + birthday + "]";

}

}

PersonSort Class:

import java.util.*;

import java.io.*;

public class PersonSort implements Comparator

{

//Global static variable for file

static final String Person_File = "./src/Persons.txt";

// Create compare method to compare two Person objects

@Override

public int compare(Person o1, Person o2)

{

return 0;

}

public static void main(String [] args)

{

String line;

String data = "";

ArrayList person = new ArrayList();

try

{

//Object for the FileReader class

FileReader fileReader = new FileReader(Person_File);

BufferedReader bufferedReader = new BufferedReader(fileReader);

//Loop to read the data in the file Persons.txt

while ((line = bufferedReader.readLine()) != null)

{

data += line;

}

}

catch (FileNotFoundException ex)

{

//Print if file cannot be opened

System.out.println("Unable to open file '" + Person_File + "'");

}

catch (IOException ex)

{

//Print if file cannot be read

System.out.println("Error reading file '" + Person_File + "'");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!