#unable to open or read the file

25 messages · Page 1 of 1 (latest)

half python
#

    // 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 + "]";
    }
}
pastel badgerBOT
#

This post has been reserved for your question.

Hey @half python! Please use /close or the Close Post button 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.

half python
#
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

pseudo crypt
#

are you sure the path for the file is correct ?

half python
#

I put this code in replit

pseudo crypt
# half python 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

half python
#

Ok hold on

#

Do u want me to send a screenshot

pseudo crypt
#

yeah.

half python
#
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
pseudo crypt
half python
pseudo crypt
#

where is your input.txt ?

half python
#

Wait give me a minute

#

🤦🏽‍♂️

#

I got it working now

#

I forgot the input

pseudo crypt
#

there wasn't an input.txt file, was there xD

half python
#

No 🤦🏽‍♂️

pseudo crypt
#

Well glad we could figure this one out :)

half python
#

Thank you 🙏

pastel badgerBOT
# half python 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.

pastel badgerBOT