Question

// Copy text file and insert line numbers Create a NetBeans project named AddLineNumbers following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans project AddLineNumbers and before you attempt to execute your application download and/or copy the text data file datalnput.txt from the course Blackboard to the AddLineNumbers project folder 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(Stringl args) Write the code for the main class as indicated below: package addlinenumber /* mport 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 /* Instantiate Scanner object for data input from file datalnput.txt. There should be no path specification as your input file should be located in your AddLineNumbers project folder*/ // Your code here

I have the files set up in the write place. I am just confused on how to use some of the desire methods to execute this code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package AddLineNumbers;

import java.io.*;
import java.util.*;


public class AddLineNumber{

    public static void main(String[] args){

          try {
              FileInputStream fstream = new FileInputStream("dataInput.txt"); //Assuming the input filename as textfile.txt
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String strLine;
              File fout = new File("dataOutput.txt");
              FileOutputStream fos = new FileOutputStream(fout);
              PrintWriter writer = new PrintWriter(fos);

              int count = 0;
             
              while ((strLine = br.readLine()) != null)   {
                  count++;
                  writer.println( Integer.toString(count) + "." + strLine );
              }
              System.out.println("Output file has been written");
              br.close();
              writer.close();
          }
          catch (FileNotFoundException e)
          {
               e.getMessage();
               e.toString();
               e.printStackTrace();
          }
          catch (IOException e)
          {
               e.getMessage();
               e.toString();
               e.printStackTrace();
          }
    }
}

Add a comment
Know the answer?
Add Answer to:
I have the files set up in the write place. I am just confused on how...
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
  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • Hey I really need some help asap!!!! I have to take the node class that is...

    Hey I really need some help asap!!!! I have to take the node class that is in my ziplist file class and give it it's own file. My project has to have 4 file classes but have 3. The Node class is currently in the ziplist file. I need it to be in it's own file, but I am stuck on how to do this. I also need a uml diagram lab report explaining how I tested my code input...

  • ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button...

    ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button to a frame in the FXBookQuote2 program. When the user clicks the button, display the title of the book that contains the opening sentence or two from your favorite book in the label. FXBookQuote2.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters...

    Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters in it (see ExtraneousStringException.java. This java file does not need to be modified). Copy paste the contents of ExtraneousStringException.java into a new .java file, uncomment the commented out code, and write your code inside the main method. In the driver class, keep reading strings from the user until the user enters “DONE.” You may read your input from a file, or use the console....

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • Ok So I have the code I need! and it runs on online codechef IDE! The...

    Ok So I have the code I need! and it runs on online codechef IDE! The code is below: import java.util.Scanner; import java.io.FileWriter; public class Main { public static void main(String[] args) {    Scanner sc = new Scanner(System.in);    double sales; System.out.print("Enter annual sales: "); sales = sc.nextDouble();    double fixed = 30000;    double commission = (7.0*sales)/100.0;    double total = fixed + commission;    System.out.println("Fixed Salary: "+fixed); System.out.println("Annual Sales: "+sales); System.out.println("Commission: "+commission); System.out.println("Total Salary: "+total);      ...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

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