Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I would like to create a CountAdder class whose objects have a counter and a method that takes a String and adds the latest counter
I would like to create a CountAdder class whose objects have a counter and a method that takes a String and adds the latest counter value to the beginning of it. You shouldn't hesitate to do whatever is necessary to get that processing out of the I/O code.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileOutputStream;
public class HasNextLineUsingResourcesDemo { public static void main(String[] args) { try (Scanner inputStream = new Scanner(new FileInputStream("ch10/original.txt")); PrintWriter outputStream = new PrintWriter(new FileOutputStream("ch10/numbered.txt")); ) { int count = 0; while (inputStream.hasNextLine( )) { String line = inputStream.nextLine( ); count++; outputStream.println(count + " " + line); } } catch(FileNotFoundException e) { System.out.println("Problem opening files."); e.printStackTrace(); System.exit(0); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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