Question

Lab exercise Computing III 9 1. There is text file (testl.txt) which contains multi-line data including the name John at multiple lines as follows. John is a good student. He studies regularly for 8 hours daily and attends to every lecture in school. John wants to be a computer programmer John is also a great sportsman. Open a new file (test2.txt) replacing the name “John with Smith 2. Assume that a text file (text3.txt) stores employee data as follows. 1001, John, Jones, Sales, 10000 1002, Jane,Smith, Production, 8000 1003, Keith,Marshall CustomerService,7000 1004,Pat, Wallace,Sales,9000 The fields are separated by comma. The fields are employee number, first name, last name, department name and salary Write a program which fetches the first name and salary of the employee based on input of employee number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program 1:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class StrReplOne extends Exception{
  
public static void main(String args[])
{
try
{
// simply opens the file of .txt format
File file = new File("D:\\a\\file1.txt");  
// just like Scanner, BufferedReader reads data from a file
BufferedReader read = new BufferedReader(new FileReader(file));  
// create a temporary string to store each line "eachline"
// create another string to maintain the structure of the data from text file "everyline"
String eachline = "", everyline = "";
// now read every line till end_of_line i.e one complete row, use while
// the readline() reads the data from the text file
while((eachline = read.readLine()) != null)
{
// after each end_of_line, a new line should come, so use "\r\n"
// "\r\n" is used to enter a new line in files i.e. carriage return and new line
everyline += eachline + "\r\n";
}
// after all the line are read and stored, close the file
read.close();

// To replace a word everywhere in a file, use replaceAll()
// the "newtext" stores the same structured text file with replaced names
String newtext = everyline.replaceAll("John", "Smith");
  
// To write a text file as it is, use FileWriter to open a file
// and also use it to write the data
FileWriter write = new FileWriter("D:\\a\\file2.txt");
write.write(newtext);
// again close the file after the use
write.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}

Screenshot for program 1:

// you can change the d:\\a\\file1.txt to simply file1.txt

Program 2:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class StrReplTwo {

public static void main(String[] args) {

try {
// read input from the user
Scanner scan = new Scanner(System.in);
System.out.println("Enter employee number : ");
// next() is used to read input as string
String empno = scan.next();
// BufferedReader in = new BufferedReader(new FileReader("D:\\a\\file3.txt"));
File file = new File("D:\\a\\file3.txt"); // or just file3.txt
BufferedReader read = new BufferedReader(new FileReader(file));
// we need a string to temporarily store each line and then check the empno
String s;
while ((s = read.readLine()) != null) {
// split() is used to seperate each word by a delimiter
// "each[]" is now an array string that stores each word in a line
String[] each = s.split(",");
//Since data format is employee number,first name,last name,designation,salary
//each[0] holds the employee number, each[1] holds the first name
//and each[4] holds the salary
if (each[0] == empno) {
System.out.println(each[1] + " " + each[4]);
//if we got the output, then no need to chech other lines, so break the while
break;
} else {
//if no match found for empno, then this executes
System.out.println("no match found");
}
}
read.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

The screen shot for program 2:

// please make sure input file is created already

// COMMENT FOR DOUBTS

Add a comment
Know the answer?
Add Answer to:
Lab exercise Computing III 9 1. There is text file (testl.txt) which contains multi-line data including...
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
  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

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