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”}
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.
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.
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...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name of a text file. The file should consist of a sequence of integers, one integer per line. The program will read in each line (using nextLine()), parse it as an int (using Integer.parseInt()), and report the number of values and their average. The average should be computed as a double. Your program should do some basic exception handling: (1) If the file cannot be...
Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit...
Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit ID# and a phone# in the format of ###-###-####. The data were generated randomly so there might be duplicates in the IDs. You're asked to do the following by using standard library algorithms as much as possible: 1. read the file into a map which has an integer for key (ID#) and a string for value (phone#), this allows the duplicates to be removed....
IN JAVA: Write code (using ArrayLists) to read a line of input from the user and...
IN JAVA: Write code (using ArrayLists) to read a line of input from the user and print the words of that line in sorted order, without removing duplicates. For example, the program output might look like the following: Type a message to sort: to be or not to be that is the question Your message sorted: be be is not or question that the to to
Write a program in C that takes a file name as the argument on the command...
Write a program in C that takes a file name as the argument on the command line, and, if the file is a symbolic link, prints out the contents of that link (i.e. the file name that the link points to). If it is not a symbolic link, the program should print an error int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR :...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT