Question

Write this in Java. say i have a file called "file.txt" that looked like this: Dunn...

Write this in Java.

say i have a file called "file.txt" that looked like this:

 Dunn        Sean        31111
Duong       Geoffrey    29922
Fazekas     Nicholas    31100
Prezioso    Stefano     22223
Puvvada     Mohana      11224

and i have an object class called Student, with three String fields lastName, firstName, and ID.

i want to read in file.txt from another class (preferably using BufferedReader) and store each line in the file as a Student in a Student array

(example: first line gets stored as ("Dunn" , "Sean" , "31111") in a Student array students as students[0])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any queries write a comment if understood upvote.

SOLUTION:

I have used two files one for code and one for storing student details

This will read data from file: FIle name "ReadFIle"

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ReadFile {
   public static void main(String[] args) {
   ArrayList<Student> students =new ArrayList();
   //array list to store data
   try
   {
   File file=new File("C:/Users/vinod/OneDrive/Desktop/file.txt"); //give file path to do operations
   FileReader fr=new FileReader(file); //reads the file
   BufferedReader br=new BufferedReader(fr); //creates a buffering character input stream
   StringBuffer sb=new StringBuffer(); //constructs a string buffer with no characters
   String line;
   while((line=br.readLine())!=null) //read data line by line till end
   {
   String[] details=line.split("\\s+");
   //split the line read based on space if we use " " it will take only one space
   //for that i used regex of \\s+ it will indicate any number of spaces
   Student student=new Student(details[0],details[1], details[2]);
   students.add(student);
  
   }
   fr.close(); //closes the stream and release the resources
   for(int i=0;i<students.size();i++) {//print the array data by using to string method
       System.out.println(students.get(i).toString());
   }
   }
   catch(IOException e)
   {
   e.printStackTrace();
   }
  
   }
}

Student Class :

package fine;

public class Student {
   public String lastName, firstName,ID;

   Student(String lastName,String firstName,String ID){
       this.lastName=lastName;
       this.firstName=firstName;
       this.ID=ID;
   }

   @Override
   public String toString() {
       return "Student [lastName=" + lastName + ", firstName=" + firstName + ", ID=" + ID + "]";
   }

}

CODE IMAGE: "FileRead"

STUDENT FILE:

OUTPUT:

Inout file:

Output:

Add a comment
Know the answer?
Add Answer to:
Write this in Java. say i have a file called "file.txt" that looked like this: Dunn...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • Part I (20%) [File: Student.java] Create a class called Student that has the following stored properties:...

    Part I (20%) [File: Student.java] Create a class called Student that has the following stored properties: • StudentID • First Name • Last Name Class Student should have read/write properties, constructor(s) and should implement the Academic interface. For academic methods, return zero for average, zero for credits and false for graduate. Also implement the toString() method that returns the above information as a String. Part II (5%) [File: Academic.java] Create an interface Academic that declares three methods: 1. average -...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • This is a java homework for my java class. Write a program to perform statistical analysis...

    This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT