Help correcting java program. I'll give you all the information so you understand completely. original question: Write a program that reads a file (provided as
Help correcting java program. I'll give you all the information so you understand completely.
original question:
Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Throw an exception if the file does not exist. An error message should result.
For example output should be:
1. This file contains
2. lines of text to
3. determine if you can
and so on and so fourth. The original .txt file reads:
This file contains lines of text to determine if you can properly read and write from a file.
The code im using reads:
import java.util.Scanner; import java.io.FileWriter; import java.io.File;
public class scanning { public static void main(String[] args)
{
//storing names of input/output files
final String txtIn="Lab4AInputfile.txt", txtOut="InputNumbered.txt";
try
{
//opens file for input Scanner in=new Scanner(new File(txtIn));
//opens file for output FileWriter out=new FileWriter(new File(txtOut));
int lineCount=1; //stores current line number
while(in.hasNextLine()) //while input file has next line to read
{
String line=in.nextLine(); //reading line
out.write(lineCount+". "+line+" "); //writing line with line number
lineCount+=1; //updating line number
}
//closing input and output files in.close(); out.close();
}
catch(Exception e) //catching exceptions
{
System.out.println(e); //error message
}
} }
My question is, where do I insert the .txt file,which is called "Lab 4 Input file.txt", to resolve this error?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started