Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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? image text in transcribed

scanning (Java Application) C:\Program Files\ava\jre1.8.0_20\bin\javaw.exe (Feb 21, 2020, 10:11:08 AM) java.io.FileNotFoundException: Lab4AInputfile.txt (The system cannot find the file specified) module 4 lab 4A > JRE System Library [JavaSE-1.8] . src >> # (default package)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students explore these related Databases questions