Question

Write a Java Program, that opens the file "students.txt" The program must read the file line...

Write a Java Program, that opens the file "students.txt"

  • The program must read the file line by line
  • The program parses each line that it reads
  • For example, for this line:

1:mohamed:ali:0504123456:cs102:cs202

  • The program must print
  •    >ID = 1
  •    >First Name = Mohamed
  •   >Last Name = Ali
  •   >Mobie = 0504123456
  •   >Courses = cs102, cs202
  • In addition, it adds the mobile phone number into an ArrayList called studentPhoneList
  • Print the content and the size of studentPhoneList

Show your results and provide your code.

txt file contain this

1:mohamed:ali:0504123456:cs102:cs202:cs330
2:ahmed:hamdi:0507415985:cs460:cs320
3:kamel:salem:0503245714:cs140
4:Mohsen:Mohamed:0503245714:cs140:cs150

simple code

Homework Answers

Answer #1

Note the directory structure. I have a

Project
|->src
|->FileManupulation.java
|->students.txt

The java file and the text file are in same dierctory

FileManpulation.Java

package file.manupulation;

import java.io.IOException; //contains exceptions the input output exception handling
import java.nio.file.Files; //java.nio.file is the API to handle input output operation.It was included in JAVA 7
import java.nio.file.Path; //Contains operations for file paths
import java.nio.file.Paths; //Contains operation for file paths
import java.util.ArrayList; //to use the ArrayList
import java.util.List; // to use the List interface

class Student{ //student class stores individual student information
   public String id;
   public String firstName;
   public String lastName;
   public String mobile;
   public List<String> courses;
   public Student(String id,String firstName,String lastName,String mobile) { //the constructor. We don't add the courses through it
       this.id = id;
       this.firstName = firstName;
       this.lastName = lastName;
       this.mobile = mobile;
       this.courses = new ArrayList<String>();
   }  
}

public class FileManupulation {
   public static void main(String[] args) throws IOException {
       Path pathOfFile = Paths.get("./src/students.txt");           //"./src/students.txt" is relative path to the students.txt file. You can mention the absolute path instead.
       List<String> lines = Files.readAllLines(pathOfFile);        //stores all the content of the file as list of strings
       List<Student> students = new ArrayList<Student>();         //List of all the students
       List<String> studentPhoneList = new ArrayList<String>();    //this the list for storing the phone numbers
       for(String details:lines) {
           String[] components = details.split(":");                                               //we take out different components of the students using ":" as delimiter
           String firstName = components[1].substring(0, 1).toUpperCase() + components[1].substring(1);     //to make the first name start with upper case
           String lastName = components[2].substring(0, 1).toUpperCase() + components[2].substring(1);       //to make the last name start with upper case
           Student student = new Student(components[0],firstName,lastName,components[3]);                   //stores id,first name,last name,mobile
           studentPhoneList.add(components[3]);                               //store the phone number in studentPhoneList                      
           students.add(student);
           for(int i=4;i<components.length;i++)                   //adding the courses
               student.courses.add(components[i]);
       }
       /*printing all the student details*/
       for(Student student:students) {
           System.out.println(">Id = "+ student.id + "\n>First Name = " + student.firstName + "\n>Last Name = " + student.lastName + "\n>Mobile = " + student.mobile );
           System.out.print(">Courses = " + student.courses.get(0));
           for(int i=1;i<student.courses.size();i++)
               System.out.print(","+ student.courses.get(i));
           System.out.println("\n");
       }
          
      
   }
}

Refer to the below images for proper indentaion and output

Know the answer?
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
Similar Questions
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
java program; Write a brief program that writes your name to a file in text format...
java program; Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
THE CODE MUST BE PYTHON # create a file with the name <last_three_digits_of_your_empl_id>.txt # write into...
THE CODE MUST BE PYTHON # create a file with the name <last_three_digits_of_your_empl_id>.txt # write into this file two lines: # 1. your name # 2. your hobby # close this files. # # open the created files <last_three_digits_of_your_empl_id>.txt, # add a third line to this file with today's date. # close the file.
Java File I/O Write a brief program that writes your name to a file in text...
Java File I/O Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. code below import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class ReadingFiles { public static void main(String[] args) throws FileNotFoundException { System.out.println(totalEven); } }
Write an assembly program that reads characters from standard input until the “end of file” is...
Write an assembly program that reads characters from standard input until the “end of file” is reached. The input provided to the program contains A, C, T, and G characters. The file also may have new line characters (ASCII code 10 decimal), which should be skipped/ignored. The program then must print the count for each character. You can assume (in this whole assignment) that the input doe not contain any other kinds of characters.  the X86 assembly program that simply counts...
Write a brief program in JAVA that writes your name to a file in text format...
Write a brief program in JAVA that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.