Hello! I have an assignment for my Intro to Java Programming class and I would l
ID: 3602066 • Letter: H
Question
Hello! I have an assignment for my Intro to Java Programming class and I would like some help on it please! It is listed below!
Write a code that extracts lines from line number 709 to 714 from a romeo_and_juliet.txt in a zip file attached and that dumps those extracted lines into a file called "output.txt". The following are the lines that should be printed in output.txt. The line numbers are not in the file remeo_and_juliet.txt. They are just added for explanation.
709: Wife. Nurse, where's my daughter? Call her forth to me.
710: Nurse. Now, by my maidenhead at twelve year old,
711: I bade her come. What, lamb! what ladybird!
712: God forbid! Where's this girl? What, Juliet!
713:
714: Enter Juliet.
I cannot attatch the .txt file unfortunately. But thank you for any help in advance.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class RomeoJuliet {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter from and to line numbers which you want to extract:");
System.out.println("Enter from line number:");
int from=sc.nextInt();
System.out.println("Enter to line number:");
int to=sc.nextInt();
BufferedReader reader=new BufferedReader(new FileReader(new File("romeo_and_juliet.txt")));
String line="";
int counter=0;
BufferedWriter writer=new BufferedWriter(new FileWriter(new File("output.txt")));
while((line=reader.readLine())!=null){
counter++;
if(counter>=from && counter<=to){
writer.write(line+" ");
}
if(counter>to)
break;
}
reader.close();
writer.close();
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.