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 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.
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...
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
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....
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
1.      Create 100 text files automatically; in each file write a random number from 1 to 10....
1.      Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….) 2.      Read the content of the 100 files and combine them into a 1 single file. 3.      Write java code to do the following: a.      To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW b.      Read the file using a java program c.      Find how many D’s in the file d.      Extract the text between the @ and the # 1. Create 100 text files...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT