Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java NetBeans project named AddLineNumbers with the main class named Ad

ID: 3866522 • Letter: C

Question

Create a Java NetBeans project named AddLineNumbers with the main class named AddLineNumbers . This program will read data from a text file "dataInput.txt" and while there are more records, prepend a line number to the text data and output the concatenated data to "dataOutput.txt" implementing a try/catch block to catch any file errors thrown by the JVM. After you have created your NetBeans project AddLineNumbers and before you attempt to execute your application download and/or copy the text data file dataInput.txt from below to your AddLineNumbers project folder. * PLEASE FOLLOW THE INSTRUCTIONS AND STRUCTURE AS STRICTLY AS POSSIBLE. *

After you have created your NetBeans Project your application class should contain the following executable code: package addlinenumbers; public class AddLineNumbers f public static void main(String[I args)f Write the code for the main class as indicated below: package addlinenumbers; /* Import the Scanner class and all classes from java.io*/ // Your code here public class AddLineNumbers public static void main( String [ args ) /* Declare variable identifiers to handle text data input from the file and count/increment the linenumber * //Your code here /* Start try block */ // Your code here

Explanation / Answer

dataInput.txt

public class Hello
{
public static void main(String [] args )
{
System.out.println("Hello");
}
}

____________________

AddLineNumbers.java

package addlinenumbers;


//Importing the Scanner class and all classes from java.io.*;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.Scanner;

public class AddLineNumbers {
public static void main(String[] args) {

//Declaring variables
int count = 1;
String str = "";
Scanner sc = null;
FileOutputStream fout = null;
PrintWriter writer = null;
// Scanner object is used to get the inputs entered by the user

//Start of try block
try {
/*
* Instantiante Scanner Object for data input from dataInput.txt .
* There should be no path specification as your input file should
* be located in your AddLine numbers
*/
sc = new Scanner(new File("dataInput.txt"));

/* Instantiate a FileOutputStream object for file "dataOutput.txt"
* Do not specify a path .Your file will be created in the project folder
*/
fout = new FileOutputStream("dataOutput.txt");

/*
* Instantiate a PrintWriter Object for writing data to your FileOutputStream
*/
writer = new PrintWriter(fout);

//Reading the contents of the file
while (sc.hasNext()) {
str += count++;
str += " ";
str += sc.nextLine();
writer.write(str);
writer.println();
str = "";
}

} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//Closing the files
sc.close();

writer.close();
}
}

}

____________________________

dataOutput.txt

1 public class Hello
2 {
3 public static void main(String [] args )
4 {
5 System.out.println("Hello");
6 }
7 }

_______________________Could you rate me well plz.Thank You