Need help filling in the areas, unsure of how to do the code, JAVA. I\'ve highli
ID: 3723898 • Letter: N
Question
Need help filling in the areas, unsure of how to do the code, JAVA. I've highlited what needs completing.
import java.io.File ; import java.io.FileNotFoundException ; import java.io.PrintWriter ; import java.util.Scanner ; import javax.swing.JFileChooser ; import javax.swing.JOptionPane ; /** LineNumbererChooser shows how to use the JFileChooser. You will set it to the file "input1" in the current directory. The program reads input1 and prepends each line with a line number. For example, if the following is the input file (input1): Mary had a little lamb whose fleece was white as snow. Every where that Mary went, the lamb was sure to go. Then the following is the output file (ignoring the // comment symbols) */ // /* 1 */ Mary had a little lamb // /* 2 */ whose fleece was white as snow. // /* 3 */ Every where that Mary went, // /* 4 */ the lamb was sure to go. public class LineNumbererChooser { //-----------Start below here. To do: approximate lines of code = 1 // 1. public static void main( ... so that file IO is possible without catching a possible FileNotFoundException //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. { JOptionPane.showMessageDialog(null, "In the next file dialog box, please choose the file: input1") ; JFileChooser jfc = new JFileChooser(".") ; //set to current directory File infile = null ; if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { //-----------Start below here. To do: approximate lines of code = 1 // 2. set infile to the file returned by message getSelectedFile() sent to jfc //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } else infile = new File("LineNumbererChooser.java") ; Scanner scanner = new Scanner(infile) ; PrintWriter pw = new PrintWriter("output1") ; int lineNumber = 1 ; //-----------Start below here. To do: approximate lines of code = 4 // 3-6. Write the while loop that goes threw the input file and prints the output file line by line with the commented line numbers. //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. long length1 = infile.length() ; pw.close() ; scanner.close() ; //This next part is just to show what is in the output file infile = new File("output1") ; scanner = new Scanner(infile) ; while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()) ; } long length2 = infile.length() ; if (length2 <= length1) System.out.println("Error: You have to print to the given output file.") ; } }
Explanation / Answer
please find my answers below for each of 3 parts.
1. public static void main(String[] args) throws FileNotFoundException
2. infile = jfc.getSelectedFile();
3. while (scanner.hasNextLine())
{
pw.println("/* " + lineNumber + "*/ " + scanner.nextLine());
lineNumber++;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.