Need help with this java practice question. Need to complete main method.
Adding line numbers to the file.
1. Begin with the supplied file Template.java.
public class Template {
static final String INPUT_FILE = "test.txt";
static final String OUTPUT_FILE = "output.txt";
public static void main(String[] args) {
}//main
}
2. Complete the main method so that it will read in the supplied
file test.txt and write out a modified version of this file as
output.txt. (These two names are predefined for you as global
constants.) The output file should be the same as the input file,
except that every line should have a line number added to the
front, in the format shown below. The supplied file test.txt
contains:
This is the top line
The previous one is blank.
Short one.
This is the last one.
Your program should create an output file output.txt which
contains:
1: This is the top line
2:
3: The previous one is blank.
4: Short one.
5: This is the last one.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// Template.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Template {
static final String INPUT_FILE = "test.txt";
static final String OUTPUT_FILE = "output.txt";
public static void main(String[] args) throws FileNotFoundException {
// opening input file to read. make sure test.txt file exist in the
// current working directory / root directory of your project. otherwise
// the system will throw exception.
Scanner scanner = new Scanner(new File(INPUT_FILE));
// opening output.txt file to write
PrintWriter writer = new PrintWriter(new File(OUTPUT_FILE));
// variable to store current line number
int lineNumber = 1;
// looping as long as there is more data to read in the file
while (scanner.hasNext()) {
// reading next line
String line = scanner.nextLine();
// writing line number followed by a colon and space and then the
// read line.
writer.println(lineNumber + ": " + line);
//updating line number
lineNumber++;
}
//closing files, saving changes
scanner.close();
writer.close();
}// main
}
Need help with this java practice question. Need to complete main method. Adding line numbers to...
Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result. Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...
Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...
I need help writing the following java Java Method below. I just need a method to take a user inputted First or Last Name and return the associated phone number, thank you! import java.util.*; import java.io.*; class PhoneList { public static void main(String[] args) throws Exception { boolean loop = true; File file = new File("PhoneList_Data.txt"); FileWriter fw = new FileWriter(file, true); fw.write("PhoneList Data"); fw.write("\n" + "Last, First: 6041337567, 2181731566"); // do { } (loop) fw.close(); } //...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
Need help Purpose Calculate mileage reimbursements using arrays and methods. The Mathematical Association of America hosts an annual summer meeting. Each state sends one official delegate to the section officers’ meeting at this summer session. The national organization reimburses the official state delegates according to the scale below. Write a Java program to calculate the reimbursement values, satisfying the specifications below. Details on array and method usage follow these specs. 1. The main method should declare all the variables at...
Java
Description This project is designed to help you practice for the second midterm examination. First you will hand write the code for the project, just as you would do on the examination. Then you will take your hand written code and enter it into eclipse and debug/fix it. This should help you identify challenges that you have in hand writing code that could cause problems on the midterm on Wednesday Write a program that generates platitudes, by writing each...
java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...
Modify the following given Java program. You are expected to modify the supplied Exercise1.java to print just the last lines of each paragraph in HTML format. Copy and paste the Sample input on the console then print the Expected output. import java.util.Scanner; public class Exercise1 { /** A simple static string for HTML & table header. */ private static final String HTMLHeader = "<!DOCTYPE html>\n" + " <html>\n" + " <head>\n" + " <title>CSE: Exercise 1</title>\n" + " </head>\n" +...
Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1. The input and variable value file are both text files that will be specified in...
Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...