I\'m having problems getting the program to read the file I have in the src fold
ID: 3879022 • Letter: I
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 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<MyDate> {
//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<Person>
{
//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> person = new ArrayList<Person>();
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 + "'");
}
}
}
Explanation / Answer
The problem here is You are not performing any operation
while ((line = bufferedReader.readLine()) != null)
{
data += line;
}
System.out.println("Combined data is: " + data);
Data has all the Persons in Combined FORM, After that You dont do anything.
What you should do it, Make person object add to Array List and print the array List first. Then Try to sort them Later
Let me Add Print statement so that You will get Some Output.
After that Just seperate date and Person name and make Person Object
Add below line after while loop
System.out.println("Combined data is: " + data);
You will see some output
while ((line = bufferedReader.readLine()) != null)
{
data += line;
}
System.out.println("Combined data is: " + data);
Comment for any help, I will respond. And please let me know how the data is stored in Persons.txt.
Is it Name<comma> Date ?
Or Name <Space> Date
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.