// creating the instance variables as defined
private int employeeID;
private String lastName;
private String firstName;
private int age;
// generating a default constructor and a parameterized constructor
public Employee() {
}
public Employee(int employeeID, String lastName, String firstName, int age) {
this.employeeID = employeeID;
this.lastName = lastName;
this.firstName = firstName;
this.age = age;
}
// accessors and mutators
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// to string method for string representation of class
@Override
public String toString() {
return "Employee [Employee ID=" + employeeID + ", last name=" + lastName +
", first name=" + firstName + ", age=" + age + "]";
}
}
#unable to open or read the file
25 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @half python! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
// main method to read the data from file and store the input in a array of class
// then displaying the array as output
public static void main(String[] args) {
// Actual data structures like linked list or array list can be used
// to store the data
List<Employee> employees = new ArrayList<>();
// reading the data from the file using Scanner object
try {
// since the file opening can cause exception
Scanner sc = new Scanner(new FileInputStream("input.txt"));
// reads until there is data in the file
while (sc.hasNextLine()) {
// constructing the objects from the data read and storing in the list
// reading the line at a time and splitting using the comma
String line[] = sc.nextLine().split(",");
// reading the int, String, String, int in order from the line
int employeeID = Integer.parseInt(line[0]);
String lastName = line[1];
String firstName = line[2];
int age = Integer.parseInt(line[3]);
// adding the employee object from the read data
employees.add(new Employee(employeeID, lastName, firstName, age));
}
// closing the scanner
sc.close();
} catch (FileNotFoundException e) {
System.out.println("Unable to open or read the file");
}
// finally displaying the list
for (Employee employee : employees) {
System.out.println(employee); // displaying each employee in the list to console
}
}
}
This up here is the main function, the employee file is all the way up top
are you sure the path for the file is correct ?
I put this code in replit
from the looks of it this code should work fine. However, if you didn't reference the file correctly it won't work. Can you share the file structure of the project
yeah.
Given the following file (you’ll need to type this into a text file in Repl)
12345,Jones,Michael,45
45432,Smith,Mary,21
98034,Lee,YiSoon,34
48223,Thompson,Zaire,39
29485,Mendez,Jorge,61
Employee Class:
Note, the file will be in the following order:
Employee ID, Last Name, First Name, Age
Make instance variables for the following:
Employee ID (Integer), Last Name (String), First Name (String), age (Integer)
Create assessor methods for each instance variable:
String getFirstName(), String getLastName(), int getEmpID(), int age()
Create mutator methods for each instance variable:
void setFirstName(String first), void setLastName(String last), void setEmpID(int id), void setAge(int age)
Create a toString() method that will print out each record like this:
Employee firstName = YiSoon
Employee lastName = Lee
Employee ID = 98034
Employee Age = 34
Implement the Comparable interface and create the Comparable method:
public int compareTo(Employee other)
In this method, compare the last names.
If the last name of the calling object is the same as the other object return 0
If the last name of the calling object is less than the other object return -1
If the last name of the calling object is greater than the other object return 1
I need the file structure present in replit. Something like
Project
--src
---Employee.java
--out
--test
where is your input.txt ?
there wasn't an input.txt file, was there xD
No 🤦🏽♂️
Well glad we could figure this one out :)
Thank you 🙏
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.